Forum Moderators: phranque
I'm trying to work out how to redirect my old URLs to new, rewritten URLs.
I've read this forum post: [webmasterworld.com...] which covers the situation I have however I can't get it working and there was no clear conclusion/answer to this post.
My code is as followed:
RewriteEngine On
RewriteRule ^/filepath/blah/blah/?$ [domain.com...] [R=301,L]
RewriteRule ^rewritten/path/?$ filepath/blah/blah/ [NC,L]
This results in:
- the rewritten url (domain.com/rewritten/path) works and successfully loads the original URL path (disuising the URL correctly).
- the original URL does not 301 redirect to the rewritten URL
Any help on this would be much appreciated!
Funny thing is, I had this working a few weeks ago, then randomly it stopped... i'm not sure what happened.
RewriteEngine on
#
# Externally redirect direct client requests (only) for filepath (old URL) to new URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /filepath/blah/blah/?\ HTTP/
RewriteRule ^filepath/blah/blah/?$ http://www.example.com/rewritten/path/ [R=301,L]
#
# Internally rewrite new URL to filepath
RewriteRule ^rewritten/path/?$ filepath/blah/blah/ [L]
Jim
[edited by: jdMorgan at 12:56 am (utc) on Mar. 17, 2009]
.
There were three problems with your rewrite (even though you said it worked).
1. Your rewrite fires for both non-www and www requests. That is a Duplicate Content issue.
You need a site-wide 301 redirect ahead of that rewrite to fix the URLs to be www if they are requested without it.
2. By having a question mark after the slash you are setting it up so that two different URLs (one with slash, one without) resolve to the same content, That is a very bad idea.
Ahead of your general non-www to www redirect (discussed in point 1) you should have another redirect that redirects "with slash" to "without slash" while fixing up the www in the domain at the same time for those requests (to prevent a redirection chain for 'with slash and without www' requests).
3. The [NC] flag would allow URL requests containing any case to activate the rewrite. That leads to yet more duplicate content issues. As shown, jd correctly deleted the [NC] flag from the rewrite so that only lower-case URL requests can get the content.
If you have upper-case, or mixed-case, URL requests coming in for that content, then you need to redirect those requests to a fully lower-case URL before the rewrite.
.
It is fine for your redirect to have a question mark after the slash. With the redirect your are forcing both old URLs to redirect to the single new URL. It is a bad idea to have the question mark present in a rewrite.
g1smd could you please explain to me what you mean by direct client requests?
Essentially I need this redirect to work, as the links on the website itself are still going to point to the OLD URLs. The reason being is that we cannot have the links changed on the site due to the structure of the Content management system (it's beyond our control). Would this redirect still work in this situation?
I will be taking care of the WWW/non-WWW with another few lines of htaccess, I just didn't include this in my example
Thanks again
Having every user pass through a redirect every time they click a link within the site is grossly inefficient (two HTTP transactions for every page view) and could very well confuse search engines - every link on your site links to a URL which sends a redirect to somewhere else. That is far from ideal.
Direct Client Request - means URL requests coming from a browser or spider, not as a result of internal pointers being updated during a rewrite.