Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

mod rewrite - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...