【问题标题】:Query string in a mod_rewrite rule (.htaccess)mod_rewrite 规则中的查询字符串 (.htaccess)
【发布时间】:2014-06-15 21:06:06
【问题描述】:

如果我的 q= 参数中有问号,.htaccess 会将我重定向到 404 错误页面。

这是我的 .htaccess 重写规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^search/(.*)/(.*)/(.*)$ results.php?q=$1&start=$2&type=$3 [L,QSA]

有人知道如何在 get 参数中添加问号吗?

【问题讨论】:

    标签: php .htaccess mod-rewrite query-string


    【解决方案1】:

    尝试改用这个。它将处理对search/ 的所有请求。从三个路径(如您的示例所示)到一个路径:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^/search/([a-z0-9]+)/?([^/]*)$ /results.php?q=$1&$2 [QSA]
    RewriteRule ^/search/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ /results.php?q=$1&start=$2&type=$3 [L]
    RewriteRule ^/search/([a-z0-9]+)/([a-z0-9]+)/?$ /results.php?q=$1&start=$2 [L]
    RewriteRule ^/search/([a-z0-9]+)/?$ /results.php?q=$1 [L]
    

    或者在网址前面不带/ 试试这个:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^search/([a-z0-9]+)/?([^/]*)$ results.php?q=$1&$2 [QSA]
    RewriteRule ^search/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ results.php?q=$1&start=$2&type=$3 [L]
    RewriteRule ^search/([a-z0-9]+)/([a-z0-9]+)/?$ results.php?q=$1&start=$2 [L]
    RewriteRule ^search/([a-z0-9]+)/?$ results.php?q=$1 [L]
    

    第二个适用于我的本地 MAMP 设置。如果我创建一个results.php,其内容如下:

    <?php
    
    echo '<pre>';
    print_r($_GET);
    echo '</pre>';
    
    ?>
    

    然后加载以下网址;我根据您的评论指出您在/search/google/0/web 上遇到错误:

     http://localhost:8888/search/google/0/web
    

    这是我在浏览器中得到的结果:

    Array
    (
        [q] => google
        [start] => 0
        [type] => web
    )
    

    【讨论】:

    • 我收到此错误消息:“在此服务器上找不到请求的 URL /search/google/0/web。”
    • @user3478635 好的,更新了我的答案。请尝试使用第二个示例。在对 URL 进行测试时使用 MAMP 在 Mac OS X 中为我工作:http://localhost:8888/search/google/0/web
    • @Elias35 URL 中的问号是一个查询字符串。原始发布者在将 URL 方案传递给查询字符串时遇到问题。
    • 下一个失败是未定义的 GET 类型,如果我在 url 中输入问题。你知道为什么 ? (www.example.com/search/google/0 ?/0/web)
    • @Elias35 是的,因为问号是查询字符串,并且由于 SEO 友好的 URL 将数据传递到查询字符串目标,所以您不能将问号(查询字符串)传递给另一个问号(查询字符串)。
    猜你喜欢
    • 2011-06-02
    • 2016-03-26
    • 2011-04-18
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多