简体   繁体   中英

URL rewriting in PHP when several values are being passed in the URL

I am currently researching how to modify URLs in php using Mod rewrite.

A typical URL could look like this:

http://www.fitness.com/find_a_pt/?county=&constituency=211&gender=&action=search

Now in the above example only a constituency has been selected. No county or gender has been specified.

Now the above constituency refers to 'Dartford'.

So it would be good if the URL read:

http://www.fitness.com/find_a_pt/dartford

Now the added complication is that a gender may be either:

1) Not selected - as per the URL above

2) Male

http://www.ego3fitness.com/find_a_pt/?county=&constituency=211&gender=1&action=search

3) Female

http://www.ego3fitness.com/find_a_pt/?county=&constituency=211&gender=&actiom=search

So the URLs would need to read:

1) http://www.fitness.com/find_a_pt/dartford

2) http://www.fitness.com/find_a_pt/dartford/male

3) http://www.fitness.com/find_a_pt/dartford/female

Firstly is it possible to be this specific with the URL rewrites and if so could someone provide an example for me to work from.

Thanks for your help in advance.

I always use the same mode_rewrite rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

With this rules everything is forwared to index.php. So you are free to implement every url logic with PHP.

You can get the request uri with $_SERVER['REQUEST_URI'] and do whatever you want.

It is nice to have a Routing class with regex rules to parse the uri. Look at an example here and also read how the big frameworks like Zend, Code Igniter etc. do it. (The rewrite rule I provided is from Zend Framework by the way)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM