Forum Moderators: phranque
[edited by: phranque at 4:36 am (utc) on Jan 9, 2014]
[edit reason] unlinked url for clarity [/edit]
[edited by: phranque at 4:37 am (utc) on Jan 9, 2014]
[edit reason] unlinked url for clarity [/edit]
I just read this in the forum: 'Always give the full protocol-plus-domain in the target of a redirect. Otherwise some requests will get redirected twice if they came in asking for the wrong form of the domain name.' I suspect that helps, but I'm not sure how to implement it.
RewriteEngine On
# redirect user agent requests for index.html or default.asp in any directory to directory index (trailing "/")
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index\.html|default\.asp)\ HTTP/
RewriteRule ^(([^/]+/)*)(index\.html|default\.asp)$ http://www.example.com/$1 [R=301,L]
# redirect non-canonical hostname variants to www.example.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.html? [NC]
RewriteRule ^(.*/)?index\.html?$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} /default\.asp? [NC]
RewriteRule ^(.*/)?default\.asp?$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} index|default RewriteRule ^(([^/]+/)*)(index\.html|default\.asp)$ et cetera
1.I'm assuming this [A-Z]{3,9} means any letter from a to z or number from 3 to 9, but why not 0,1,or 2?
2. I understand ^ starts the pattern, but do I not need $ to end it in RewriteCond?
3. I don't entirely understand the purpose of this: \ /([^/]+/)* OR \ HTTP/
How does [^./] do that?
2. I understand ^ starts the pattern, but do I not need $ to end it in RewriteCond?
[^.]+ ^[^.]$ 3. I don't entirely understand the purpose of this: \ /([^/]+/)* OR \ HTTP/
4.I love the idea of getting out sooner:)! How does [^./] do that?
5.From your explanation, should my rewrite look like this?
RewriteCond %{THE_REQUEST} index|default
RewriteRule ^(([^/])*)(index|default) http://www.example.com/$1 [R=301,L]
^(([^/]+/)*)(index|default) 6. And why drop HTTP/ from phranque's example?
66.249.84.204 - - [08/Jan/2014:02:03:19 -0800] "GET /favicon.ico HTTP/1.1" 200 1750 "-" "Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0 Google favicon"
GET /favicon.ico HTTP/1.1
^GET\ /favicon\.ico\ HTTP/1\.1$
GET\ /favicon.ico\ HTTP/1.1 [A-Z]{3}\ /favicon\.ico\ HTTP/1\.[01]
[A-Z]{3,9}\ /\w+\.ico\ HTTP/1\.[01]
[A-Z]{3,9}\ /[a-z]+\.ico\ HTTP/1\.[01]
favicon\.ico
\.ico
HTTP/1\.1
[A-Z]{3,9}\ /[.\s]+\.\w+
And why drop HTTP/ from phranque's example?
Again, this is the minimalist version. You only need to match things that actually occur, so you can sometimes save time by being slapdash.
(index|default)\ HTTP/
(index\.html|default\.asp)\ HTTP What are the spaces around /([^/.]+/)*(index|default)for? start and end?
In mod_rewrite, there is only one syntactic element, and that's the blank space " ".
Does this combination of both of your suggestions keep the code specific while achieving the objective of one redirect instead or two (my initial problem) and with a more consolidated version than my original?
Was the [NC] dropped from your examples for security reasons?
And I read to chmod .htaccess to 644, which I did - is it secure now?