Forum Moderators: phranque
[size=2]Options +FollowSymLinks
RewriteEngine On
# redirect index.htm and index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]
# redirect no-www to www.
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L][/size]
[edited by: g1smd at 8:52 pm (utc) on Oct 15, 2012]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L] [edited by: g1smd at 9:15 pm (utc) on Oct 15, 2012]
I predict this question will be asked again within the next ten days. :)
\.html? pattern matches both .html and .htm requests. Redirect both. I actually use \.(html?|php)$ or \.(html?|php[45]?)$ ? after hostname paired with !^ and $ stops an infinite redirect loop for pure HTTP/1.0 requests. Pure HTTP/1.0 requests do not include a host header. ^example\.com$ pattern doesn't allow a redirect for requests for example.com:80 or www.example.com:80 nor many other non-canonical hostnames. (.*) is greedy and captures "everything" so you don't need to tell it start at the beginning and continue to the end. It does that on its own. [edited by: g1smd at 9:59 pm (utc) on Oct 15, 2012]
index.php4 or for index.php5 ever? If so, include those as options using [45]? here. [L] flag stops rule processing once "this" rule matches. Every rule should have this flag.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
[edited by: ewwatson at 11:09 pm (utc) on Oct 15, 2012]
the .* seems pretty typical I thought.
I'm finding that we have posted the right answer so many times, that Google now treats them all as Duplicate Content and prefers to show all the various versions of wrong answers instead.
every week for the last ten years
no $ sign after the php?