Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

mod_rewrite index page to just the domain

want to rewrite domain.com/index.shtml to just domain.com

3:53 pm on May 16, 2005 (gmt 0)

10+ Year Member

I have tried the following things that do not work:
redirect 301 /index.shtml http://www.example.com

redirect 301 http://www.example.com/index.shtml http://www.example.com

RewriteCond %{HTTP_HOST} ^domain\.com/index.shtm
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

I have tried several variations of the above examples and I get one of two results, either a 500 internal server error, or no redirect or rewrite.

I already have the following things working:

AddHandler server-parsed .htm .html .shtml
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

RewriteCond %{QUERY_STRING} ^([A-Za-z0-9])(.*)$
RewriteRule ^(.*)?(.*)$ http://www.example.com/$1? [R=301,L]

the problem is that google has my main index page indexed several times, like this:


the things I have working now are getting rid of the ppc tracking urls and also the www/non-www problem but I can't seem to find a solution for the index.shtml problem

[edited by: jdMorgan at 4:05 pm (utc) on May 16, 2005]
[edit reason] Examplified. [/edit]

7:29 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The following rewritten code should be equivalent to what you already have, adding the /index.shtml redirect.

AddHandler server-parsed .htm .html .shtml
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Redirect client requests for /index.shtml to "/" in main doamin
# (This will not affect internal rewrites)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.shtml
RewriteRule ^index\.shtml$ http://www.example.com/ [R=301,L]
# Redirect all requests for alternate domain names to 'favored' domain to prevent duplicate content in SEs
# (Removed the filename from your RewriteCond - it will not be present in %{HTTP+HOST})
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Remove query string (guessing -- not sure if this is what you intended).
RewriteCond %{QUERY_STRING} ^[a-z0-9] [NC]
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

Make sure that you have no links on your site to "index.shtml" -- Otherwise, you will always have problems with serach engines requesting it and getting redirected. Always link to "/" and eventually, the /index.shtml listing should go away.

If the /index.shtml fix above still results in a redirection loop, then rename the /index.shtml file to something else and add an internal rewrite from "/" to the new file. This should prevent the loop. The loop, if any, would be a result of the interaction of mod_dir, and mod_rewrite.

%{THE_REQUEST} is the original request header line received from the client. It might look like this, for example:

GET /widgets.shtml?color=blue&texture=fuzzy HTTP/1.1

Testing %{THE_REQUEST} in a RewriteCond should guarantee that the RewriteRule will only be invoked if an HTTP client requests /index.shtml, and will not be invoked if /index.shtml is accessed as the result of an internal redirect (internal redirects cannot change the HTTP request header).


8:05 pm on May 16, 2005 (gmt 0)

10+ Year Member


Thanks for the help, this works great! Is there anything I can add or modify to do one more thing?
all of the rewrites work properly and return the proper header responses, the only thing that does not work is if you go to:

it only rewrites to:


and I would like it to go to, or rewrite:


it works as long as the query string url has the www in it though.

8:50 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I assume there's a typo in your post, because you say that "A" only rewrites to "A".
However, that code should remove query strings and redirect domains, no matter what. I suggest you flush your browser cache and try testing again. Otherwise, the typo has me confused.


10:23 pm on May 16, 2005 (gmt 0)

10+ Year Member

sorry, let me try to explain it again.

when I input: [example.com?query=string...]

it rewrites and 301's to: [example.com...]


when I input: [example.com?query=string...]
(notice, no www)
it goes to: [example.com?query=string...]

11:08 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Well, referring to the code I posted above, the non-www to www domain redirect should take place. This is a 301-redirect, as specified in the rule. This ends the current HTTP transaction, and begins a new client transaction. This time, the client is asking for the correct www domain, but still has the query string.

So now, the third rule should kick in, and once again redirect the client to the www domain, but now without the query string.

If you're asking can you do it all at once, sure, but then you'll end up with rules for all possible combinations of domain names and query strings -- more rules for you to write and maintain, and more rules for the server to process on every request...

So, is the problem that your browser does not end up at the proper URL eventually, or is it that you want to do all redirects at once?

If it's not redirecting eventually to the right place, then make sure you didn't inadvertently change anything about the code above except for the domain name. Most notably, the pattern anchors are present when needed, and missing when not desired. I've reviewed it several times, and it should work as you desire, albeit using two separate redirects.



Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month