.htaccess causes redirection

Viewed 79

Hello there I have 2 folders inside public_html ex.(mainfolder and subfolder). Inside my public_html same level with my 2 folders, I have an .htaccess file like this.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Options -Indexes

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^paw\.xevz\.migor\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.paw\.xevz\.migor\.eu$
RewriteRule ^/?$ "http\:\/\/paw\.xevz\.migor\.eu\/mainfolder" [R=301,L]

This .htaccess will redirect the page to mainfolder. When I try to view my subfolder like this http://paw.xevz.migor.eu/subfolder , everything is working well but since I encountered some 404 error when trying to view some functions because there is no index.php in the url. I added this .htaccess inside my subfolder.

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

It is working well but I found out that when I try to open, http://paw.xevz.migor.eu/subfolder as I did before, it will redirect me to the main folder. In order to view my code, I have to view the link like this. http://paw.xevz.migor.eu/subfolder/somefunction. Can someone explain why this is happening ? I don't have much knowledge in .htaccess and I'm not sure what is going on. What will I do in my laravel project, or in my .htaccess to avoid this conflict ? so that when I want to view my subfolder, I will need to put this in the url ? http://paw.xevz.migor.eu/subfolder without redirecting . Thank you.

2 Answers

Have this rule in your root .htaccess:

# using THE_REQUEST make sure we match whitespace (\s) 
# followed by 1+ / followed by ? or \s
RewriteCond %{THE_REQUEST} \s/+[?\s]
RewriteCond %{HTTP_HOST} ^(?:www\.)?paw\.xevz\.migor\.eu$ [NC]
RewriteRule ^ /mainfolder/ [R=302,L]

THE_REQUEST variable represents original request received by Apache from the browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1 or GET / HTTP/1.1

Make sure you clear your browser cache before testing this change.

You can exclude a folder from redirection by adding this line to your .htaccess:

RewriteRule ^ (yourFolderName) ($ | /) - [L]
Related