Forum Moderators: phranque
Options +FollowSymLinks
RewriteEngine on
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Externally redirect direct client requests for "<any-directory>/index.html" and # "<any-directory>/index.htm" to "<any-directory>/" RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.html?.*\ HTTP/
RewriteRule ^(([^/]*/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
# Externally redirect to fix up FQDN and appended port numbers
#RewriteCond %{HTTP_HOST} ^example.com(\.|:[0-9]*) [NC]
#RewriteRule (.*) http://www.example.com/$1 [R=301,L]
example.com/index.html and see that you are redirected to www.example.com/index.html and then on to www.example.com/ ^(([^/]*/)*)index\.html?$
example.com///folder///folder/folder///index.html Redirect if NOT www.example.com (exactly) to www.example.com
http://www.example.com/index.html
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
HTTP/1.1 301 Moved Permanently
Date: Sun, 23 Oct 2011 20:43:46 GMT
Server: Apache/2.2.3 (CentOS)
Location: http://www.example.com/
Content-Length: 247
Connection: close
Content-Type: text/html; charset=iso-8859-1
Options +FollowSymLinks
RewriteEngine on
# Externally redirect to fix up FQDN and appended port numbers
RewriteCond %{HTTP_HOST} ^example.com(\.|:[0-9]*) [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Externally redirect direct client requests for "<any-directory>/index.html" and # "<any-directory>/index.htm" to "<any-directory>/" RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.html?.*\ HTTP/
RewriteRule ^(([^/]*/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
http://www.example.com/index.html
GET /index.html HTTP/1.1
Host: www.example.com
http://example.com/index.html
GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
HTTP/1.1 301 Moved Permanently
Date: Sun, 23 Oct 2011 21:18:04 GMT
Server: Apache/2.2.3 (CentOS)
Location: http://www.example.com/index.html
Content-Length: 257
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com/index.html
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
HTTP/1.1 301 Moved Permanently
Date: Sun, 23 Oct 2011 21:18:04 GMT
Server: Apache/2.2.3 (CentOS)
Location: http://www.example.com/
Content-Length: 247
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com/
GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
HTTP/1.1 200 OK
Date: Sun, 23 Oct 2011 21:18:04 GMT
Server: Apache/2.2.3 (CentOS)
Accept-Ranges: bytes
Content-Length: 20733
Connection: close
Content-Type: text/html
----------------------------------------------------------
Options +FollowSymLinks
RewriteEngine on
# Externally redirect to fix up FQDN and appended port numbers
# Externally redirect direct client requests for "<any-directory>/index.html" and "<any-directory>/index.htm" to "<any-directory>/"
RewriteCond %{HTTP_HOST} ^example.com(\.|:[0-9]*) [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.html?.*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine on
# Externally redirect direct client requests for "<any-directory>/index.html" and "<any-directory>/index.htm" to "<any-directory>/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.html?.*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine on
# Externally redirect direct client requests for "<any-directory>/index.html" and "<any-directory>/index.htm" to "<any-directory>/"
RewriteCond %{THE_REQUEST} index\.html?\ HTTP
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
should resolve to the same URL, but currently do not.
index\.html?(\?[^\ ]+)?\ HTTP/ [edited by: g1smd at 12:06 am (utc) on Oct 24, 2011]
Options +FollowSymLinks
RewriteEngine on
# Externally redirect direct client requests for "<any-directory>/index.html" and "<any-directory>/index.htm" to "<any-directory>/"
RewriteCond %{THE_REQUEST} index\.html?(\?[^\ ]+)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
# Redirect if NOT www.example.com (exactly) to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Options +Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteBase /
RewriteCond %{THE_REQUEST} ^[c-t]{3,9}\ /index\.html?(\?[^\ ]+)?\ HTTP/ [NC]
RewriteRule ^(.*)index\.html /$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.html?.*\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.html?$ http://www.example.com/$1? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
[NC] flag from the first RewriteCond, otherwise the rule doesn't fix incorrectly cased requests. RewriteCond pattern should begin ^[A-Z]{3,9}\ /([^/]+/)*index otherwise it only works for root index requests. (.*) at the beginning of a pattern. Use ^(([^/]+/)*)index here. ([^/]*/)* should be ([^/]+/)* in two places. html?.*\ HTTP/ in the first RewriteCond should be html?(\?[^\ ]+)?\ HTTP/ here. Never use .* at the beginning or in the middle of a RegEx pattern. [NC] from the second RewriteCond. [R=301,L] in one rule and [L,R=301] in the other. While they both do exactly the same thing, you should get into the habit of always using one style. This makes typos easier to spot.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?(\?[^\ ]+)?\ HTTP/ [NC]
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]