PHP rewrite rules

Actual URL that my application uses:

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

but I would like this to be achieved with

 http://site.com/iPhone 

I have no experience rewriting rules, how can I customize this?

The solution works, but the new URL appears in the address bar. I thought it would be possible to customize this so that it looks as if the page layout

http://site.com/iPhone

without changing the display

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

Is it possible? Thanks.

+7
.htaccess mod-rewrite
source share
3 answers

Create a file called .htaccess in the root directory of your site and put it in it.

 RewriteEngine on Options +FollowSymlinks RewriteBase / RewriteRule ^(.*) search.php?search=$1 [R] 

Gotta do the trick.

I would suggest, however, that you make this a little more specific, so maybe you need a search directory user in your url. for example, instead of mysite.com/IPhone use mysite.com/search/IPhone, which will work as

 RewriteEngine on Options +FollowSymlinks RewriteBase / RewriteRule ^search/(.*) search.php?search=$1 [R] 

This makes it easier to work with regular pages that are redirected to arnt, for example, about us or the base page.

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

+10
source share

You need to specify something like this in the .htaccess file:

 RewriteEngine on RewriteRule /(.*) /search.php?search=$1 

Check also:

+5
source share

The rewrite rules are not part of PHP as far as I know, but Apache (in particular mod_rewrite ) or any other server that you use. For Apache, you need to have a file on the server named .htaccess , and it has something like:

 RewriteEngine on RewriteRule ^(\w+)/?$ /index.php?search=$1 

^(\w+)/?$ is a regular expression - it matches any word with 1 or more characters followed by / . Therefore, it changes site.com/iPhone to site.com/index.php?search=iPhone . That sounds good?

+3
source share

All Articles