Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm stuck in a big problem, but let me start with one part of it.

I am wodering why this works:

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

while this doesn't:

RewriteCond %{REQUEST_FILENAME} index\.html [NC]
RewriteRule ^(.*) main.redirect.php [QSA,L]

And this one doesn't work as well:

RewriteRule ^(.*)index.html main.redirect.php [NC,QSA,L]

Any ideas?

I'm using this as part of a main problem, where the customer wants me to only do one redirect for either one or all of the following conditions

  • www is missing (put it in front)
  • index.html is at the end (remove it)
  • CAPS are present (convert to lowercase)

To achieve this, I created a php file which does all that in one go, and I need to call that on either one of the conditions above (the php file will do the redirect then):

echo '$sURL = ' . $sURL . '<br />';
$sURL = strtolower($sURL);
if (substr($sURL, 0, 4) != 'www.') {
    $sURL = 'www.' . $sURL;
if (substr($sURL, -10) == 'index.html') {
    $sURL = substr($sURL, 0, -10);
echo 'Location: http://' . $sURL;
#header('Location: http://' . $sURL, true, 301);

(the echo lines are for testing purposes, they will be removed and the header() command activated later)

In the .htaccess file, I now have the following (right after "RewriteEngine On" and "Options +FollowSymlinks":

    # 2014-07-16 RM: Exclude these files from rewriting
    RewriteRule \.(js|ico|gif|jpg|jpeg|png|css|pdf)$ - [NC,QSA,L]

    # 2014-07-16 RM: Check if the url starts with www
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*) main.redirect.php [QSA,L]

    # 2014-07-16 RM: Check if the url ends with index.html
#    RewriteCond %{SCRIPT_FILENAME} ^(.*)index\.html$ [NC]
#    RewriteRule ^(.*) main.redirect.php [QSA,L]
    RewriteRule ^(.*)index.html main.redirect.php [NC,QSA,L]

    # 2014-07-16 RM: Check if the url contains upper-case characters
#    RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
#    RewriteRule ![A-Z] - [E=HASCAPS:FALSE,S=1]
#    RewriteCond %{ENV:HASCAPS} TRUE
#    RewriteRule ^(.*) main.redirect.php [QSA,L]

Any help is gladly appreciated!

Kind regards



I stripped the .htaccess part down to a few lines, but even in this case only the www-check works as expected:

RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{REQUEST_URI} ^(.*)/index\.html$ [NC,OR]
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*) main.redirect.php [QSA,L]
share|improve this question

I haven't tested this, however the first thing I spotted was that in your RewriteRule lines which are like this:

RewriteRule ^(.*) main.redirect.php [QSA,L]

You should try adding a slash before main.redirect.php as that part of the redirect rule should be a URL, so it would become:

RewriteRule ^(.*) /main.redirect.php [QSA,L]
share|improve this answer
Thx for the answer, but as I stated above, it works for the www-check part, so this can't be the problem. – Spider IT Sep 3 '14 at 5:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.