Can anybody help me? I’m using this simple statement to redirect the root index.htm to the root directory /
Redirect 301 /index.htm /
But this causes an infinity loop. It redirects to my root, so it does the job. But now it’s impossible to send requests to the root without getting an error telling you something about infinite redirects. My question: Why? And how to avoid this?
€dit: Now I have exactly this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [NC,QSA,L]
RewriteRule ^((.*)/)?index.[(php)(htm)(html)] $1
[R=301,L]
If i comment out one of these rules, the other runs perfectly. However, having both of these rules causes a rewrite to the root in every case. Please help!
€dit 2:
What I want is handling all incoming request with one single index.php file and redirecting every index.php, index.htm and index.html to it’s directory because of duplicate content. How to do that?
€dit 3:
Now everything works like a charm!
RewriteEngine On
RewriteBase /
#Redirects http://host.tld/gamedev to http://gamedev.host.tld/
Redirect 301 /gamedev/ http://gamedev.host.tld/
#Redirects every request to a subdirectory's index file to the subdirectory because of duplicated content
RedirectMatch ^/(.*)/index.(php|html?)$ http://gamedev.host.tld/$1
#Remove index file from a root folder request
RewriteCond %{THE_REQUEST} s/index.(php|html?)
RewriteRule ^index.(php|html?)$ / [R=302,L]
#Handling all incoming requests with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Thanks a lot
Darth Moon
2
Answers
I don’t think you can to this with a “Simple Redirection”. The reason why you are getting the infinity loop is that the your apache is tring to deliver the content of the file
/index.htm
when you accessing the root directory/
because it’s part of the directory index list. Now your redirection takes effect (because you virtual accessing the page/index.htm
) and the procedure starts from the beginning.To solve your problem you should use a rewrite rule instead of a redirection. The rewrite rule only affects to the requested URL and doesn’t end in an infinity loop. The following rule should work in your case:
Because after the first rule has rewritten the request to
index.php
, the rewritting process starts over and the second rule then strips everything from the URL.Your second rule is incorrect. It’s not matching what you thinks it’s matching and is unnecessarily matching against any URL-path. You stated that this only needs to match the index document in the root.
[(php)(htm)(html)]
– Presumably the intention is to match any of these file extensions, but instead this matches a single character:p
,h
,t
,m
orl
. To matchphp
,htm
orhtml
you need to use alternation. For example:(php|htm|html)
or(php|html?)
. (The?
makes thel
optional in the 2nd pattern.)These directives are also in the wrong order. The external redirect should be before your front-controller.
@Benjamin’s answer is pretty much on the button, except for the condition (that will never match) and that this must go at the top of your
.htaccess
file.Try the following instead:
The
QSA
orNC
flags were not required on your rewrite.The reason for checking against
THE_REQUEST
in the first condition is to prevent a redirect loop (in some situations).THE_REQUEST
contains theHost
header from the initial request and does not change after URL rewriting.Test with
302
(temporary) redirects and only change this to301
(permanent) when you are sure this is working OK. 301s are cached hard by the browser so can make testing problematic. As always, you must ensure your browser cache is clear before testing.