Forum Moderators: phranque

Message Too Old, No Replies

rewrite/301 problem

         

DiscoStu

6:14 pm on Apr 1, 2010 (gmt 0)

10+ Year Member



So I have an old url

domain.com/41413?oldurl

and it's being rewritten to

domain.com/clean-url/

Problem is now that they both resolve and some traffic still goes to the old url. How do I best take care of this? I tried doing a 301 to the clean url:

redirect 301 /41413?oldurl htp://www.domain.com/clean-url/


(I'm aware that one t is missing from http here, it's not like that in the live htaccess)

But this causes an internal server error. I was under the impression that you could 301 an old url to a new one, then rewrite the new url to grab the content from the old url - but I guess this creates some type of loop? What is the best way to deal with this?

g1smd

6:19 pm on Apr 1, 2010 (gmt 0)

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



You need to use RewriteRule with the [R=301,L] flags, not Redirect. The target will again be a URL with domain name included.

Precede that with a RewriteCond looking at THE_REQUEST so that you know you're looking at the literal HTTP Request sent by the browser, and not the result of a previous rewrite.

DiscoStu

9:34 pm on Apr 1, 2010 (gmt 0)

10+ Year Member



Thanks for the reply. I'm still very hazy on the RewriteCond but here's what I'm trying now


RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /41413?oldurl
RewriteRule ^41413?oldurl$ clean-url/ [R=301,L]


It seems to be working the way I want to. I didn't fully follow your instructions - I found an example on a website and tried it - but does this look legit to you?

As I understand it, it's saying "301 the old url to the clean one ONLY if the request is specifically for the old url".

So together with the original rewrite rule it looks like this:


RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /41413?oldurl
RewriteRule ^41413?oldurl$ clean-url/ [R=301,L]

RewriteRule ^41413?oldurl/?$ clean-url/ [L]


Is there a better/more standard way of doing it? You mentioned including the domain name which I left out, is it necessary? Thanks again for the help, I don't get to work with this stuff often but it's very interesting

g1smd

11:35 pm on Apr 1, 2010 (gmt 0)

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



RewriteRule cannot see the query string. You'll need to extract that either from THE_REQUEST or from a RewriteCond looking at QUERY_STRING.

The redirect target should also include the protocol and domain name.

The rewrite code you have now is exactly backwards. It should be accepting the clean URL request and rewriting it to the internal script location.

jdMorgan

12:27 am on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The description of the process and the goals is a bit muddled, but with explicit comments added, you're likely trying to do something like this:

# Redirect direct client requests for old dynamic URL to new clean static-looking URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /41413\?oldurl
RewriteRule ^41413$ http://www.example.com/clean-url/? [R=301,L]
#
# Internally rewrite requests for clean URL to script filepath
RewriteRule ^clean-url/$ /41413?oldurl [L]

Jim

DiscoStu

3:58 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



Thanks to both of you, I've implemented and it seems to be working. Babysteps :P