Forum Moderators: phranque
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.
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?
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 activatedRule #2: ^name/(.*)\.html$ MATCH - Rewrite to /name.php?id=00010
RESTART APACHE URL-FILENAME API PHASEURI: 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 activatedRule #2: ^name/(.*)\.html$ NO MATCH - Fails on "html" subpattern
SERVE REQUESTED CONTENT from URL-path /name.php?id=00010
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
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!