首页 > 解决方案 > .htaccess 重写多查询字符串,只有一个查询作为路径

问题描述

我尝试从 .htaccess 重写

https://example.com/?page=contact&query1=test1&query2=test2

https://example.com/contact/?query1=test1&query2=test2

输出为

Array
(
    [page] => contact
    [query1] => test1
    [query2] => test2
)

我尝试了下面的重写,但未能获得输出-

RewriteEngine On
RewriteRule ^(.*?)//?(.*)$ /?page=$1&$2 [NC,L]

在这里我应该包括我可以在https://example.com/contact/之后进行任何查询,输出如下,类似于带有“ page ”查询的常规获取字符串。

如-

https://example.com/?page=contact2&query15=test14&query25=test24&query35=test34

将输出 $_GET 查询字符串

Array
(
    [page] => contact2
    [query15] => test14
    [query25] => test24
    [query35] => test34
)

标签: phpapachemod-rewritequery-string

解决方案


以下规则应该可以解决问题:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/?([^/]+).* /?page=$1&%1 [NC,L]

我们检查打开的链接是否不是有效/现有的文件或目录,然后我们捕获当前的查询字符串,最后我们重写访问的页面。

请参阅:https ://htaccess.madewithlove.be?share=1e473616-5364-5718-8819-a8032a5319c1


推荐阅读