重定向规则以使用斜杠/ .htaccess文件更改url参数

时间:2017-10-30 07:49:13

标签: .htaccess

我需要用斜杠更改url参数。

我目前的工作网址是

https://www.example.com/Blog-Details.php?blog=test-title

我需要像下面那样改变它

https://www.example.com/Blog-Details.php/blog/test-title

我的htaccess文件是

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteRule ^([a-z_]+).html $1.php
RewriteRule ^([A-Za-z_-]+).html $1.php
RewriteRule ^([a-z]+)/([a-z]+)/(.*)\.html /market_material.php?ind=$3
#RewriteRule ^([A-Z]{2})/([\+a-z\ ]+)\.html$ /reports_content.php?
report_id=$1 
#RewriteRule ^report/([0-9]+) /reports_content.php?report_id=$1
RewriteRule ^([0-9]+)/(.*)\.html /reports_content.php?report_id=$1
#RewriteRule ^blog/([0-9]+) /Blog-Details.php?blog=$1
#RewriteRule ^Blog-Details.html/([^/]+)?$ $1.php?blog=$2 [L]
RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]

</IfModule>

我添加了这一行来转换带斜杠的参数

RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]

1 个答案:

答案 0 :(得分:0)

尝试在.htaccess中使用以下RewriteRule:

RewriteEngine On
RewriteCond %{QUERY_STRING} blog=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/Blog-Details.php/%1? [R=301, NC, L]

这是使用条件抓取变量blog=,然后使用%1将其添加到您网址的末尾。您会注意到我还在RewriteRule的末尾添加了?,这是为了阻止原始查询字符串出现在URL的末尾。

使用301重定向进行重写,您可能希望将其更改为302以使其在测试时成为临时重定向。

确保在>测试之前清除缓存