Cleaning form URL

New to mod_rewrite? This is a good place to start.

Cleaning form URL

Postby Gregory » Thu Oct 01, 2009 1:51 am

Hi Guys,

I have spent the last two hours trying to figure this out so I though it's about time I asked.

I currently have this URL: http://www.example.co.uk/new/submit.php ... erties=big

and I would like it to look like this :http://www.example.co.uk/new/round-red-big

I thought this would work, but it doesn't:
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)-(.*)-(.*)\.htm$ /new/submit.php?type=$1&colour=$2&properties=$3


Thanks in advance for any help!
Gregory
 
Posts: 3
Joined: Thu Oct 01, 2009 1:37 am

Postby Gregory » Thu Oct 01, 2009 7:53 am

I have moved the files to the root, and this half works -

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-z]*)-?$                       /submit.php?type=$1                [NC,QSA,L]
RewriteRule ^([a-z]*)-([a-z]*)-?$            /submit.php?type=$1&colour=$2         [NC,QSA,L]
RewriteRule ^([a-z]*)-([a-z]*)-([a-z]*)/?$ /submit.php?type=$1&colour=$2&properties=$3 [NC,QSA,L]


However if I go to http://example.co.uk/submit.php?type=ta ... erties=big

It doesn't redirect me anywhere..
Gregory
 
Posts: 3
Joined: Thu Oct 01, 2009 1:37 am

Postby Gregory » Thu Oct 01, 2009 8:00 am

Okay, so this probably sounds really stupid, but this is doing what I want:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-z]*)-?$                       /submit.php?type=$1                [NC,QSA,R=301,L]
RewriteRule ^([a-z]*)-([a-z]*)-?$            /submit.php?type=$1&colour=$2         [NC,QSA,R=301,L]
RewriteRule ^([a-z]*)-([a-z]*)-([a-z]*)/?$ /submit.php?type=$1&colour=$2&properties=$3 [NC,QSA,R=301,L]


But in the opposite way of what I want!
Gregory
 
Posts: 3
Joined: Thu Oct 01, 2009 1:37 am

Postby richardk » Thu Oct 01, 2009 8:57 am

Your first mod_rewrite doesn't match because it's looking for .htm at the end.

However if I go to http://example.co.uk/submit.php?type=ta ... erties=big

It doesn't redirect me anywhere..

That's a totally different rewrite/redirect. To redirect the old URL you have to use RewriteConds and the %{QUERY_STRING} variable. This is a one variable example (/old.php?key=value to /new/value)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match internal requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the query string variable.
RewriteCond %{QUERY_STRING} ^(.*&)?key=([^&]+)(&.*)?$ [NC]
# Match the file and redircet to the new URL.
RewriteRule ^old\.php$ /new/%2? [R=301,L]

The more variables you add, the more complicated it gets. I advise you to redirect with PHP instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 36 guests

cron