Forum Moderators: phranque
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
redirect 301 /folder/index.html http://www.example.com/resources/
(and some other redirects)
Where do I put the RewriteRule ^\products\discontinued\.html$ - [G]? Is it after the RewriteEngine On line?
I'm a complete htaccess idiot. Please bear with me as I don't know even the simplest terminology. I have read threads here like this one [webmasterworld.com] and this Apache page [httpd.apache.org] (well, I didn't quite read it, it's mostly not in English :(). Even the nice simple explanation here [diveintomark.org] doesn't seem to answer the question.
[edited by: jdMorgan at 9:28 pm (utc) on Jan. 12, 2008]
[edit reason] example.com [/edit]
Redirect and RedirectMatch are mod_alias directives, and are not affected by mod_rewrite directives. Furthermore, the server will run all mod_rewrite directives first, followed by all the mod_alias directives, or vice-versa, depending on its configuration. So within the set of directives handled by either module, the order you specify matters, but the order of directives belonging to different modules in your file does not strictly control their execution order.
Note also that one of your slashes is backwards, and one of them shouldn't even be there:
RewriteRule [b]^p[/b]roduct[b]s/d[/b]iscontinued\.html$ - [G]
This is how it looks now with your help. Cheers.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
redirect 301 /folder/index.html http://www.example.com/resources/#File that exists no more
Redirect gone filea.html#Folder that exists no more
RewriteRule ^products/discontinued\.html$ - [G]
[edited by: jdMorgan at 9:17 pm (utc) on Jan. 12, 2008]
[edit reason] example.com [/edit]
My understanding is that redirect uses "normal" paths and rewrite uses "complicated" ones
Redirect directive uses "normal" paths because it doesn't involve the use of regular expressions. mod_rewrite is different because it does use regular expressions, which have a certain syntax and a number of reserved characters.
For example:
#Folder that exists no more
RewriteRule ^products/discontinued\.html$ - [G]
So with the final line of your example, the \ being used is an "escape" character telling Apache to treat the period . that follows it as a normal character instead of the reserved character.
The code should look like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Folder that exists no more
RewriteRule ^products/discontinued\.html$ - [G]
#
Redirect 301 /folder/index.html http://www.example.com/resources/
#
# File that exists no more
Redirect gone [b]/f[/b]ilea.html
For this reason, it would be best not to mix module usage, and instead use all mod_rewrite or all mod_alias directives. Since mod_alias cannot check the requested hostname, that means that you need to use mod_rewrite:
RewriteEngine on
#
# Non-existent URLs (Removed files)
RewriteRule ^products/discontinued\.html$ - [G]
RewriteRule ^filea\.html$ - [G]
#
# Redirect index page of /folder to /resources/
RewriteRule ^folder/index\.html$ http://www.example.com/resources/ [R=301,L]
#
# If not already redirected above, redirect non-canonical domain requests to the canonical domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Jim