.htaccess 在我的例子中使用htacces重写url
我正在使用htaccess重写我的url我的原始url是.htaccess 在我的例子中使用htacces重写url,.htaccess,url,.htaccess,Url,我正在使用htaccess重写我的url我的原始url是 http://www.example.com/products/product_detail.php?url=pro-name 我想要的url将如下所示 http://www.example.com/products/pro-name 但是我对这个url已经做了一部分了 http://www.example.com/products/product_detai/pro-name 使用这个.htaccess代码 RewriteRul
http://www.example.com/products/product_detail.php?url=pro-name
我想要的url将如下所示
http://www.example.com/products/pro-name
但是我对这个url已经做了一部分了
http://www.example.com/products/product_detai/pro-name
使用这个.htaccess代码
RewriteRule product_detail/url/(.*)/ product_detail.php?url=$1
RewriteRule /(.*) product_detail.php?url=$1
在这里,我不知道如何获得我想要的url。请帮助任何人获取所需的url。谢谢
#Assuming the correct RewriteBase is used...
#Redirect the client to the fancy url
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^product_detail\.php$ %1? [R,L]
#Rewrite the url internally and stop rewriting
#to prevent a loop
RewriteRule ^(.*)$ product_detail.php?url=$1 [END]
在这段代码中,您首先将客户端重定向到希望在地址栏中可见的url<代码>%1与重写条件的第一个捕获组匹配。尾随的?
清除查询字符串。第二条规则在内部重写url,以便服务器可以实际生成输出,而不是404错误。结束标志(可从Apache2.3.9及更高版本获得)将完全停止重写url。这是为了防止url get不断被重写的永无止境的循环。()
编辑:低于2.3.9的apache版本没有结束标志。为了防止循环,您需要解决这个问题。例如,您可以使用:
#Assuming the correct RewriteBase is used...
#Redirect the client to the fancy url
RewriteCond %{QUERY_STRING} !redirect=true
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^product_detail\.php$ %1? [R,L]
#Rewrite the url internally and stop rewriting
#to prevent a loop
RewriteRule ^(.*)$ product_detail.php?url=$1&redirect=true [L]
通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入文档根目录下的.htaccess
:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(products)/product_detail.php\?url=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^(products)/(.+?)/?$ /$1/product_detail.php?url=$2 [L,NC,QSA]
谢谢你的支持。但是这段代码有500页的错误,我刚刚测试过,代码中没有语法错误。您最有可能使用的apache版本低于2.3.9,它不会使用END
标志。您需要解决这个问题,或者需要升级您的apache版本。