Forum Moderators: phranque
I'm using an htaccess file to rewrite some URLs. I recently changed the format of these so need to use a 301 redirect to direct from the old SEO friendly address to the new.
Something not quite right tho....
eg: OLD LINK:
RewriteRule ^product/category/(.*)/(.*)/$ mytown/index.php?c=$1&d=$2 [L]
NEW LINK (illustrative):
RewriteRule ^product-category/(.*)/(.*)/$ mytown/index.php?c=$1&d=$2 [L]
So I've added this:
RedirectRule 301 ^/product/category/(.*)/(.*)/$ http://www.example.com/product-category/(.*)/(.*)/
But when i do this, it redirects to:
http://www.example.com/product-category/(.*)/(.*)/?c=$1&d=$2
any ideas?
[edited by: pageoneresults at 6:29 pm (utc) on Dec. 15, 2005]
[edit reason] Examplified URI References [/edit]
You can overcome your current difficulty by using only mod_rewrite, instead of using a mixture of mod_rewrite and mod_alias, so that you can enforce the execution order. Here's the full suite of rules needed to handle old-style URL redirects, non-canonical domain requests, static-to-dynamic rewrites, and the correction of "accidentally-exposed" dynamic URLs in the proper order:
# Externally redirect old-style static URLs to new style static URLs
RewriteRule ^product/category/([^/]+)/([^/]+)/?$ http://www.example.com/product-category/$1/$2/ [R=301,L]
#
# Externally redirect requests for non-canonical domain names
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite new-style static URLs to script
RewriteRule ^product-category/([^/]+)/([^/]+)/?$ /mytown/index.php?c=$1&d=$2 [L]
#
# Externally redirect direct client requests for dynamic URLs to new-style static URLs
# (in case your dynamic URLs accidentally get exposed to the search engines)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mytown/index\.php\?c=([^&]+)&d=([^&\ ]+)\ HTTP/
RewriteRule ^mytown/index\.php$ http://www.example.com/product-category/%1/%2? [R=301,L]
An example of {THE_REQUEST} would be:
GET /mytown/index.php?c=widgets&d=14 HTTP/1.1
I assumed that your /mytown directory is directly below document_root. If it is not, then either provide a full anchored path for the rewrites, or remove the leading slash from the rewrite substitution URLs.
Jim