Forum Moderators: phranque

Message Too Old, No Replies

Further question on how to redirect index pages to /

Redirect both index.html and index.php to "/"

         

hgupta

8:46 am on Jan 7, 2007 (gmt 0)

10+ Year Member




System: The following message was cut out of thread at: http://www.webmasterworld.com/apache/3210178.htm [webmasterworld.com] by jdmorgan - 2:27 pm on Jan. 7, 2007 (CDT -6)


I wanted 3 things to be done in my site, through .htaccess file:
-----------------------------------------------------------------
1) http://example.com >> change to >> http://www.example.com
2) http://www.example.com/index.html >> Change to >> http://www.example.com/
3) http://www.example.com/123/index.html >> change to >> http://www.example.com/123/

I wrote following codes in my .htaccess file but couldn't fix all of them; with the following code i have fixed 1st & 3rd problem, but 2nd point is still there. Please tell me how it can be written more properly to fix all of them in once.

.htaccess file have:
---------------------
RewriteEngine on

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

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=301,L]

RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php¦html)\ HTTP
RewriteRule (.*)index\.(php¦html)$ /$1 [R=301,L]
--------------------------------------------------------------

Looking forward for the correct code!

jdMorgan

8:41 pm on Jan 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is simply that rule #3 excludes example.com/index.html and example.com/index.php because it requires two slashes.

Rule #2 will only work if some other script defines the "REDIRECT_STATUS" variable; It is not an Apache-native variable. I believe this is a variable defined by Drupal (or something like that).

I'd suggest deleting your rule #2, modifying rule #3 to work properly with or without subdirectory paths prepended to index.html or index.php, and then reversing the rule order so that index page requests to the "wrong domain" don't result in a double redirect:


RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^(GET¦HEAD)\ /([^/]+/)*index\.(php¦html)\ HTTP
RewriteRule ^(([^/]+/)*)index\.(php¦html)$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

See also the notes on the use of the ".*" pattern in this recent thread [webmasterworld.com].

Replace all broken pipe "¦" characters in the code above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim