简体   繁体   中英

Rewrite variables using .htaccess

i want to rewrite my variable

../detail.php?id=910&type=car
../book.php?id=910&type=car

to

../detail/car/910
../book/car/910

but i don't know how to code it, please give me the code and the explanation. Thanks

Just go to .htaccess file make sure your RewriteEngine on then write following "RewriteRule" for your URLs:

RewriteRule ^detail/(.*)/(.*)$ detail.php?type=$1&id=$2 [NC,L]
RewriteRule ^book/(.*)/(.*)$ book.php?type=$1&id=$2 [NC,L]

If yet your rewrite rule not works then add following lines before your rewrite rule

Options +FollowSymLinks  
RewriteEngine On

Explanation of above rule:

RewriteRule - Tells Apache that this like refers to a single RewriteRule .

^detail/(.*)/(.*)$ or ^book/(.*)/(.*)$ - The pattern . The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the substitution section that follows.

detail.php?type=$1&id=$2 or book.php?type=$1&id=$2 - The substitution . If the pattern above matches the request, Apache uses this URL instead of the requested URL.

[NC,L] - Flags , that tell Apache how to apply the rule. In this case, we're using two flags. NC , tells Apache that this rule should be case-insensitive, and L tells Apache not to process any more rules if this one is used.

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