Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to new page and redirect old page

         

webdevfv

11:39 am on Jan 28, 2005 (gmt 0)

10+ Year Member



I have searched and searched and still cannot seem to get this thing to work exactly as I want and it's doing my head in.

I have the old URL:
[mydomain.com...]

and I want to 'change' it so that the new URL looks like:
[mydomain.com...]

Now I have the following code which internally redirects users who type in the new URL:
RewriteEngine On
RewriteBase /
RewriteRule ^product/(.*)/(.*)/(.*)\.html /product.php?id=$1&make=$2&model=$3 [L]

But what I also want to do is send people who type in the old URL - of course, the URL is isn't dead as it's the one being used to rewrite to the new style - to the new location. This is mainly for the search engines etc that have spidered my pages in their index.

And this is where I'm stuck big time.

Any help greatly appreciated.

Caterham

2:59 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



you can use

RewriteCond %{THE_REQUEST}  \ /product\.php\?id=([a-z0-9]+)&make=([a-z0-9]+)&model=([a-z0-9]+)
RewriteRule ^product\.php /product/%1/%2/%3.html [R=301,L]

to force an external redirect.

webdevfv

9:49 am on Feb 3, 2005 (gmt 0)

10+ Year Member



Still got a problem

I've used the code above and also bits from another thread (http://www.webmasterworld.com/forum92/2670.htm).

The internal rewrite is fine.

The external redirect seems to have a problem with my .php?id=00010 bit of code. If I add this into the RewriteRule it doesn't rewrite - and if I leave it out I get this
[xyz.co.uk...]
when what I want is this
[xyz.co.uk...]
from the original
[xyz.co.uk...]

# Externally redirect browser requests for old URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /name\.php\?id=([a-z0-9]+)
RewriteRule ^name\.php$ [xyz.co.uk...] [R=301,L]

# Internally rewrite requests to new URL
RewriteRule ^name/(.*)\.html$ /name.php?id=$1 [L]

Thanks in advance

PS Why do some rules use $1 and others %1 to define a section of replaced code?

jdMorgan

2:45 pm on Feb 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest you work with one rule at a time. Since the two rules can both affect a single user request, it will be difficult to find the problem if they're both active.

Question: Are your query string parameters always in the same order?

On your second question, $1 refers to parenthesized subexpressions from RewriteRule patterns, and %1 refers to parenthesized subexpressions from RewriteCond patterns. So, use of $1 or %1 it depends on where you want to copy from.

Here's a walk-through of what should be happening with this code in your root .htaccess (and assuming that directory "/name" does not exist).


RECEIVE NEW HTTP REQUEST
URI: http://www.xyz.co.uk/name.php?id=00010
REQUEST: "GET /name.php?id=00010 HTTP/1.1"
Cond #1: ^[A-Z]{3,9}\ /name\.php\?id=([a-z0-9]+) MATCH - Set %1=00010
Rule #1: ^name\.php$ MATCH - Redirect to http://www.xyz.com/name/00010.html
SEND REDIRECT RESPONSE (Terminates current transaction)

-New Request from redirected browser-

RECEIVE NEW HTTP REQUEST
URI: http://www.xyz.com/name/00010.html
REQUEST: "GET /name/00010.html HTTP/1.1"
Cond #1: ^[A-Z]{3,9}\ /name\.php\?id=([a-z0-9]+) NO MATCH - Fails on "php" subpattern
Rule #1: Not activated

Rule #2: ^name/(.*)\.html$ MATCH - Rewrite to /name.php?id=00010
RESTART APACHE URL-FILENAME API PHASE

URI: http://www.xyz.com/name.php?00010
REQUEST: "GET /name/00010.html HTTP/1.1" (THE_REQUEST value is unchanged)
Cond #1: ^[A-Z]{3,9}\ /name\.php\?id=([a-z0-9]+) NO MATCH - Fails on "php" subpattern
Rule #1: Not activated

Rule #2: ^name/(.*)\.html$ NO MATCH - Fails on "html" subpattern
SERVE REQUESTED CONTENT from URL-path /name.php?id=00010


It looks like it should be working.

The "URI" value shown is the current content of %{REQUEST_URI} and the URI examined by RewriteRule, which get updated by mod_rewrite on the fly.
The "REQUEST" value shown is the content of %{THE_REQUEST} which only changes when a new HTTP request is received from a client browser or bot.
RewriteCond and RewriteRule action is described.
Notes in ALL CAPS describe the server behaviour.

I'd suspect a mismatch in the form of your real URLs versus the theoretical ones we're discussing here, or a problem with the location of your code. If directory "/name" exists, then you'll need to put the rules in that subdir, or enable RewriteOptions inherit in that subdir.

Jim

webdevfv

3:12 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Jim

Thanks for responding so quickly. I'm sure the problem is because of the "?id=00010" part.

When I take this off completely and rewrite say name.php to name.html it works. So it must be something to do with the variable string.

I was thinking I might not have escaped the special characters properly.

User types in
[xyz.co.uk...]

Currently my redirected URL is
[xyz.co.uk...]

when it should read
[xyz.co.uk...]

The content is there and looks ok but the url is wrong!

jdMorgan

4:34 pm on Feb 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh... Yeah, sorry:

Put a "?" at the end of the substitution to clear the query string.


RewriteRule ^name\.php$ http://www.xyz.co.uk/name/%1.ht[b]ml?[/b] [R=301,L]

Jim

webdevfv

4:53 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Sorted. Thank you. Thank you.