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
423 views
in Technique[技术] by (71.8m points)

.htaccess - PHP Rewrite Rules

The actually URL which my app uses is:

http://site.com/search.php?search=iPhone 

but I would like it to be possible to achieve the same with

http://site.com/iPhone

I have no experience of rewrite rules, how can I set this up?

The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is

http://site.com/iPhone

without changing to display

http://site.com/search.php?search=iPhone 

Is this possible? Thanks.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Create a file called .htaccess in the root of your website and put this in it.

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule ^(.*) search.php?search=$1 [R]

Should do the trick.

I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule ^search/(.*) search.php?search=$1 [R]

This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.

As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.


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

...