简体   繁体   English

使用 .htaccess 重写变量

[英]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:只需转到.htaccess文件,确保您的RewriteEngine on然后为您的 URL 编写以下“RewriteRule”:

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 . RewriteRule - 告诉 Apache this like 指的是单个RewriteRule

^detail/(.*)/(.*)$ or ^book/(.*)/(.*)$ - The pattern . ^detail/(.*)/(.*)$ or ^book/(.*)/(.*)$ -模式 The server will check the URL of every request to the site to see if this pattern matches.服务器将检查对站点的每个请求的 URL,以查看此模式是否匹配。 If it does, then Apache will swap the URL of the request for the substitution section that follows.如果是,则 Apache 将交换请求的 URL 以用于随后的替换部分。

detail.php?type=$1&id=$2 or book.php?type=$1&id=$2 - The substitution . detail.php?type=$1&id=$2 or book.php?type=$1&id=$2 -替换 If the pattern above matches the request, Apache uses this URL instead of the requested URL.如果上述模式与请求匹配,Apache 将使用此 URL 而不是请求的 URL。

[NC,L] - Flags , that tell Apache how to apply the rule. [NC,L] - Flags ,告诉 Apache 如何应用规则。 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. NC ,告诉 Apache 这条规则应该不区分大小写, L告诉 Apache 如果使用了这条规则,不要再处理任何规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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