简体   繁体   English

使用.htaccess重写php页面的url

[英]Rewrite url of php page using .htaccess

I have one url like this 我有一个这样的网址

http://localhost/project/product_detail.php?pro_id=4&&pro_name=DH_Tiffin_with_B.B._Packing

I want to rewrite url like this 我想这样重写网址

http://localhost/project/product_detail/4/DH_Tiffin_with_B.B._Packing

Also I had changes done in xamp\\apache\\conf\\httpd.conf . 另外我在xamp\\apache\\conf\\httpd.conf进行了更改。

LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride All

I tried .htaccess code follow: 我尝试过.htaccess代码,如下所示:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([a-zA-Z0-9-]+)$ project/product_detail.php?pro_id=1&&pro_name=$1 [L,QSA]

I tried above code, but not work. 我尝试了上面的代码,但没有用。

The rewriting rule you yourself posted will never get applied to the requested URLs you cite, since the pattern won't match. 您自己发布的重写规则将永远不会应用于您引用的请求URL,因为该模式不匹配。

Here is something that should do what you ask: 这是您应该执行的操作:

RewriteEngine on
RewriteRule ^/?project/product_detail/(\d+)/(.+)$ /project/product_detail.php?pro_id=$1&&pro_name=$2 [END]

That rule should work likewise in the http servers host configuration or in a dynamic configuration file (".htaccess" style file). 该规则在http服务器主机配置或动态配置文件(“ .htaccess”样式文件)中也应同样起作用。 If you need to use a dynamic file then take care that the interpretation of such files is enabled and that above rule is placed in such a file located in the http hosts DOCUMENT_ROOT folder. 如果需要使用动态文件,请注意启用了此类文件的解释,并且上述规则位于http主机DOCUMENT_ROOT文件夹中的此类文件中。

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). 还有一个一般性说明:您应该始终喜欢将此类规则放在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. 这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare). 仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。

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

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