.htaccess with -Indexes gets into an infinite redirection loop when specifying a directory

This is my .htaccess file:

# NO LISTING OF INDEXES Options -Indexes <IfModule mod_rewrite.c> RewriteEngine On # NIX THE www BECAUSE IT IS NO LONGER 1996 AND YOU'RE COOLER THAN THAT RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)/$ http://%1/$1 [R=301,L] # NIX TRAILING SLASHES BECAUSE SEO IS A VENGEFUL GOD AND WHATNOT RewriteRule ^(.*)/$ $1 [R=301,L] # SEND ALL NON-FILE REQUESTS TO index.php FOR FIGURING OUT RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # ERROR DOCS ErrorDocument 400 /error/400 ErrorDocument 401 /error/401 ErrorDocument 403 /error/403 ErrorDocument 404 /error/404 ErrorDocument 500 /error/500 

If I try to access the folder ( /images/ or /images or some other real folder in my site structure), it gets into the redirect cycle.

I tried adding RewriteCond %{REQUEST_FILENAME} !-d before the trailing slash rule and β€œfixed” the redirection if it now forcibly terminates the slash in the corresponding folders, but also passed the folder name to my script, as if it were baked by requesting a URI for my CMS to understand.

I think now I could fix this on the CMS side by checking if the URI is a folder that someone is trying to list illegally, but ideally there would be an elegant .htaccess solution to redirect folder list calls to one of those banned pages, which I declare so well there. Does anyone know about this?

+4
source share
2 answers

Not sure if this will make a difference, but I usually start at the top before any conditional rules

 RewriteEngine On Options +FollowSymlinks RewriteBase / 

Your dubbing base starts later. Perhaps move it.

+1
source

Comment / delete this line:

 RewriteRule ^(.*)/$ $1 [R=301,L] 

You really don't need this, since you already have Options -Indexes at the top to show a forbidden error when someone tries to display the contents of your directory. Other than that, make some minor changes to your code as follows:

 # NO LISTING OF INDEXES Options +FollowSymLinks -MultiViews -Indexes <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)/$ http://%1/$1 [R=301,L] # SEND ALL NON-FILE REQUESTS TO index.php FOR FIGURING OUT RewriteRule ^index\.php$ - [L,NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule> # ERROR DOCS ErrorDocument 400 /error/400 ErrorDocument 401 /error/401 ErrorDocument 403 /error/403 ErrorDocument 404 /error/404 ErrorDocument 500 /error/500 
+1
source

Source: https://habr.com/ru/post/1432356/


All Articles