Forum Moderators: phranque

Message Too Old, No Replies

Redirecting to rewritten URL (two rules at same time)

Trying to use modrewrite to redirect old urls to rewritten URLs (cycle)

         

tensai288

9:39 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



Hi,

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.

tensai288

12:15 am on Mar 17, 2009 (gmt 0)

10+ Year Member



Just so you know, I've been reading lots of similar threads on this forum, but I haven't been able to find a clear answer - and implement a working solution. I know it must be frustrating for you guys seeing the same questions over and over!

jdMorgan

12:56 am on Mar 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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]

You will find this code posted in many threads, but a concrete example may make those previous discussions easier to find.

Jim

[edited by: jdMorgan at 12:56 am (utc) on Mar. 17, 2009]

g1smd

1:43 am on Mar 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The big clue in all this is that the redirect needs to fire only for direct client requests, examine THE_REQUEST here.

.

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.

tensai288

9:35 am on Mar 17, 2009 (gmt 0)

10+ Year Member



Many thanks for your replies guys,
JDmorgan, your example worked perfectly... I didn't realise the "THE_REQUEST" element was required (although I had tried using it whilst trying to work this out).

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

g1smd

8:47 pm on Mar 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



URLs are defined by links. You really do need to link to the new URLs.

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.