使用htaccess

时间:2018-07-24 22:08:46

标签: apache .htaccess mod-rewrite

我有一个网站example.com,并且正在URL中传递两个GET参数。

example.com/page.php?page=5&section=10

现在我要显示它

example.com/5/10

我已经将它用于第一部分,但似乎无法弄清第二部分(部分)

我当前的.htaccess文件是

RewriteEngine on

RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]

我需要得到的只是第二部分正在工作(&section = 10)

2 个答案:

答案 0 :(得分:0)

您可以使用此

RewriteEngine on

#redirect and rewrite the single get perm /?page
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]

#redirect and rewrite urls with multiple perms   
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+)&section=(\d+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

RewriteRule ^(\d+)/(\d+)/?$ page.php?page=$1&section=$2 [L,QSA,NC]

答案 1 :(得分:0)

将此添加到您的.htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} page=(.+)&section=(.+)$ [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

这将使用{QUERY_STRING}捕获变量,并将URL重写为http://example.com/5/10。在重写结束时使用?是为了防止查询在重写后追加到末尾。

在测试之前,请确保清除缓存