【问题标题】:get params with clean url in multi hosting environment在多主机环境中使用干净的 url 获取参数
【发布时间】:2012-10-05 11:21:14
【问题描述】:

网站根目录包含三个目录,例如a,b,c,它们都作为不同域的根目录

我在目录b 中创建了一个.htaccess 并在其中添加了以下规则

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]*)/?([^/]*)/$ /b/sample-page?bbi=$1&bbl=$2 [L]

上面的最后一行,我也试过了

RewriteRule ^b/([^/]*)/([^/]*)$ /b/sample-page?bbi=$1&bbl=$2 [QSA,L]

使用上述规则

http://testsite.com/b/pageone.php成功改写为

http://testsite.com/b/pageone

但是http://testsite.com/b/sample-page.php?bbi=value1&bbl=value2

被重写为http://testsite.com/b/sample-page?bbi=value1&bbl=value2 而不是

http://testsite.com/b/sample-page/value1/value2

http://testsite.com/sample-page/value1/value2

【问题讨论】:

    标签: apache .htaccess url-rewriting clean-urls


    【解决方案1】:

    您需要在其他规则中添加L 标志:

    RewriteRule ^(.*)$ $1.php [L]
    

    要处理另一种情况,您需要在之前添加一条规则。

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/b/([^/]+)/([^/]+)/([^/]+)
    RewriteCond %{DOCUMENT_ROOT}/b/%1.php -f
    RewriteRule ^/?([^/]+)/([^/]+)/([^/]+) $1.php?bbi=$2&bbl=$3 [L]
    

    这样当您转到http://testsite.com/b/sample-page/value1/value2 时,您会在/b/sample-page.php?bbi=value1&bbl=value2 获得内容

    【讨论】:

    • @RishiKalia 抱歉,在我自己的 apache 安装上测试过,在最后 2 个条件下需要 2 个 /b
    猜你喜欢
    • 2016-02-27
    • 2018-11-29
    • 2015-05-28
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多