Confused?? Multiple and Options Parameters

Discuss practical ways rearrange URLs using mod_rewrite.

Confused?? Multiple and Options Parameters

Postby kirk112 » Thu Jun 19, 2008 7:02 am

Hi

Quite new to this mod rewrite stuff, still getting my head around it all.

What I am trying to do is change the following URL
Code: Select all
http://www.example.com/browse-retail-jobs/jobs.php?location=55&job_title=32&job_id=75

to
Code: Select all
http://www.example.com/retail-jobs/xx/xx/xx/L55JT32ID75.html

(L = location, JT = job_title, ID = job_id)
The xx will be various keywords that I choose depending on the options that have been selected, I can work this out using php.

I have
Code: Select all
RewriteRule ^retail-jobs/.*/L([0-9]+)JT([0-9]+)ID([0-9]+).html$ /browse-retail-jobs/jobs.php?location=$1&job_title=$2&job_id=$3 [NC,L]


This works fine, but I want to be able to miss out any of the parameters or pass them in any combination.
Code: Select all
http://www.example.com/retail-jobs/xx/xx/xx/JT32ID75.html
job_title = 32, job_id = 75.

http://www.example.com/retail-jobs/xx/xx/xx/L55ID75.html
location = 55, job_id = 75.

http://www.example.com/retail-jobs/xx/xx/xx/ID75.html
job_id = 75.


I have tried various options but I either get stuck in a loop or end up creating 30+ rules??

Please can anyone point me in the right direction on how to achieve this.

Thanks for your help!!

Shaun
kirk112
 
Posts: 3
Joined: Thu Jun 19, 2008 6:42 am

Postby richardk » Thu Jun 19, 2008 1:54 pm

Three rules
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^retail-jobs/.*/L([0-9]+)\.html$ /browse-retail-jobs/jobs.php?location=$1 [NC,L]
RewriteRule ^retail-jobs/.*/L([0-9]+)JT([0-9]+)\.html$ /browse-retail-jobs/jobs.php?location=$1&job_title=$2 [NC,L]
RewriteRule ^retail-jobs/.*/L([0-9]+)JT([0-9]+)ID([0-9]+)\.html$ /browse-retail-jobs/jobs.php?location=$1&job_title=$2&job_id=$3 [NC,L]


Or one more complicated rule
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^retail-jobs/.*/L([0-9]+)(?:JT([0-9]+)(?:ID([0-9]+))?)?\.html$ /browse-retail-jobs/jobs.php?location=$1&job_title=$2&job_id=$3 [NC,L]

(?:...)? just makes the part contained in it optional.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kirk112 » Fri Jun 20, 2008 1:41 am

Thanks!!!!

Worked perfectly, just one final quick question,

I have a custom 404 page what I am looking to do is if any goes to retail-jobs/xxxx and the page does not exists redirect them to retail-jobs/search.php instead of the 404 page

Hope that makes sense

Again thanks for your help!!!!
kirk112
 
Posts: 3
Joined: Thu Jun 19, 2008 6:42 am

Postby richardk » Fri Jun 20, 2008 3:12 pm

/browse-retail-jobs/search.php or /retail-jobs/search.php?

For /browse-retail-jobs/search.php, try adding the following to the end of your .htaccess file
Code: Select all
RewriteRule ^retail-jobs(/.*)?$ /browse-retail-jobs/search.php [R,L]


For /retail-jobs/search.php, try adding the following to the end of your .htaccess file
Code: Select all
RewriteCond %{REQUEST_URI} !^/retail-jobs/search\.php$ [NC]
RewriteRule ^retail-jobs(/.*)?$ /retail-jobs/search.php [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Thanks!

Postby kirk112 » Mon Jun 30, 2008 6:35 am

Hi richardk

Just realised I haven't replied to your last post.

Everything works perfectly

You have saved me hours of frustration.

Thank you!
:D :D :D :D
kirk112
 
Posts: 3
Joined: Thu Jun 19, 2008 6:42 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 96 guests

cron