Too many redirects due to htaccess code

5,836

RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [R=301,L]

Yes, that line removes a trailing slash from the URL. However, you would need a condition before that to prevent a rewrite loop if mod_dir (DirectorySlash) is active and you are requesting a filesystem directory.

And that's probably the problem here. If you request /directory, where "directory" is an actual directory on the filesystem then mod_dir (specifically the DirectorySlash directive) will auto-append the trailing slash in order to "fix" the URL.

So, you request /directory/, your RewriteRule removes the trailing slash and mod_dir adds it back, etc.

However, if you are literally accessing index.html (the DirectoryIndex) on the filesystem in that directory and you have no other routing going on, then you need the trailing slash on the URL. So, it would be useful to prevent directories from being stripped of their slashes, for example:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [R=301,L]

If it's working without then I assume there is some other rewriting going on or Drupal is routing the request.

though the trailing slashes are still being removed without it.

Maybe this isn't required after all!? It's difficult to say for sure without seeing the rest of your .htaccess file.

Also, make sure you are not seeing a cached response. 301 (permanent) redirects are naturally cached by the browser.

Share:
5,836

Related videos on Youtube

SuperTony
Author by

SuperTony

Updated on September 18, 2022

Comments

  • SuperTony
    SuperTony over 1 year

    TL;DR: What does the following line of code do? Does it remove trailing slashes from a URL?

        RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    

    I inherited a Drupal site with little knowledge of how htaccess files work.

    We have a folder, let's call it "culture" in our document root. In the culture folder we have a index.html file. So that's docroot/culture/index.html.

    In my browser, when I try to go to www.mysitename.com/culture I get a infinite redirect. After using an online redirect checker I found that the following is happening:

    http://www.mysitename.com/culture/ 301 Moved Permanently http://www.mysitename.com/culture 301 Moved Permanently ...

    The trailing slash (the real monster here) is added and removed back and forth 19 times and this is what the error says specifically:

    Too many redirects. Please try to reduce your number of redirects. Actually you use 19 Redirects. Ideally you should not use more than 3 Redirects in a redirect chain. More than 3 redirections will produce unnecessary load on your server and reduces speed, which ends up in bad user experience.

    Alas, I believe I found the issue to be this line of code:

    RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    

    I comment this line of code and the redirect no longer occurs. However, in the comments for the htaccess it's stated that this line I commented out is used to remove trailing slashes on our pages -- though the trailing slashes are still being removed without it.

    Any thoughts on what this line of code does?

  • Markus Lanthaler
    Markus Lanthaler over 8 years
    Also, if this site is SSL enabled, all SSL requests will be redirected to their non-SSL equivalents (sans trailing slashes)
  • SuperTony
    SuperTony over 8 years
    Thank you @w3d. I added the rewrite condition and that fixed the issue. As you mentioned, the trailing slash is being added in a single initial redirect. Your answer is extremely thorough and that is much appreciated!