Too many redirects due to htaccess code

Too many redirects due to htaccess code - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Too many redirects due to htaccess code error on your web browser. Problem :


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?


Solution :


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.


We hope that this article has helped you resolve the htaccess, redirects, drupal error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=