Mobile app version of vmapp.org
Login or Join
Bethany197

: Too many redirects due to htaccess code TL;DR: What does the following line of code do? Does it remove trailing slashes from a URL? RewriteRule ^(.*)/$ http://%{HTTP_HOST}/ [R=301,L]

@Bethany197

Posted in: #Drupal #Htaccess #Redirects

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

RewriteRule ^(.*)/$ %{HTTP_HOST}/ [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 mysitename.com/culture I get a infinite redirect. After using an online redirect checker I found that the following is happening:
www.mysitename.com/culture/ 301 Moved Permanently 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_HOST}/ [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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Bethany197

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

RewriteRule ^(.*)/$ %{HTTP_HOST}/ [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_HOST}/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme