Forum Moderators: phranque
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www\.mydomain\.com/$1 [R=301,L] RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.{htm|html|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(htm|html|php)$ http://example.com/$1 [R=301,L] RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.{htm|html|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(htm|html|php)$ http://example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www\.website\.com/$1 [R=301,L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.[a-z0-9]+)?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.website.com/$1? [R=301,L] RewriteCond %{THE_REQUEST} ^.*\/index\.html?
RewriteRule ^(.*)index\.html?$ http://www.domain.com/$1 [R=301,L]
OR
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.domain.com/$1 [R=301,L] RewriteEngine on
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.[a-z0-9]+)?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.website.com/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www\.website\.com/$1 [R=301,L] #1. Dump the ^.*
Yes, you can combine extensions. You don't even need to be specific about them; if someone comes along and requests /index.bzzt, you can redirect them right along with everyone else. (ONLY for redirects! When rewriting, accept only canonical URLs.)
So instead of {blahblah}index\.(php|html?) you could simply say index\.\w+ It seems safe to say that if someone asks for "index." alone, or "index.one-two-three" they are probably up to no good and can be safely 404'd. You gotta draw the line somewhere. Your RewriteCond can be even more minimalist and just say
RewriteCond %{THE_REQUEST} index
Here's a few examples:
http://mydomain.com
http://mydomain.com/index.html
http://mydomain.com/index.htm
http://mydomain.com/index.php
http://www.mydomain.com/index.php
http://www.mydomain.com/index.html
http://www.mydomain.com/index.htm
all redirect to:
http://www.mydomain.com/
http://mydomain.com/subdir
http://mydomain.com/subdir/index.html
http://mydomain.com/subdir/index.htm
http://mydomain.com/subdir/index.php
http://www.mydomain.com/subdir/index.php
http://www.mydomain.com/subdir/index.html
http://www.mydomain.com/subdir/index.htm
all redirect to:
http://www.mydomain.com/subdir/ # Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L] # Redirect non-canonical hostname to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L] (\.[a-z0-9]+)?[^\ ]*\ HTTP/ is "period followed by some characters followed by stuff that is not a space then HTTP/" and simplifies to \.[^\ ]*\ HTTP/ # Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.[a-z0-9]+)?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L] RewriteEngine on
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.[a-z0-9]+)?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
# Redirect non-canonical hostname to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L] as long as your server is receiving requests for index.html you will need a redirect to the canonical url.
RewriteEngine on
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://www.example.com/$1? [R=301,L]
# Redirect non-canonical hostname to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L] Hopefully, you have
link rel="canonical" href="http://www.example.com/categoryname/"
(shtml|html?) simplifies to s?html? Should I also put in individual 301 redirects for each category index?
([^/]+/)* and (([^/]+/)*) bits are for.