【问题标题】:Htaccess Query String ConflictsHtaccess 查询字符串冲突
【发布时间】:2014-06-13 12:05:07
【问题描述】:

我会通过使用以下 .htaccess 的查询字符串将 域、子域和请求 uri 传递给 php 文件来重写 url

# Apache configuration

    Options -Indexes
    Options -Multiviews
    Options +FollowSymLinks


# Rewrite engine configuration

    RewriteEngine on
    RewriteBase /


# Sub domain redirection (301 redirect)

    RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.0-9]+)$
    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]


# Request redirection

    RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.[^\.0-9]+$
    RewriteRule ^(.*)$ index.php?app=%2&sub=%1&req=$1 [QSA,END]

以下是一些工作示例:

http://dev.testdomain.tld/hello/world

=> /index.php?app=testdomain&sub=dev&req=/hello/world

http://www.example.tld/?var=test

=> /index.php?app=example&sub=www&req=/&var=test

但如果我有这样的事情:

http://dev.testdomain.tld/hello/world?app=FAIL&sub=FAIL&req=FAIL

=> /index.php?app=testdomain&sub=dev&req=/hello/world&app=FAIL&sub=FAIL&req=FAIL

查询字符串将用FAIL(或url查询字符串中的所有其他内容)替换域、子域和请求uri变量。 我可以停用[QSA] 标志,但我想保留原始查询字符串,并防止app, sub, req 变量重写。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    您可以从查询字符串中删除 app、sub 和 req 参数,并使用 PHP $_SERVER var。这个 PHP 超全局包含一些有趣的值来解决您的问题:

    http://dev.testdomain.tld/hello/world?app=FAIL&sub=FAIL&req=FAIL
    
    var_dump($_SERVER);
    

    会导致如下输出:

    array(...) {
      ["HTTP_HOST"]=>
      string(13) "test.test.tld"
      ["QUERY_STRING"]=>
      string(9) "hahah=w12"
      ["REQUEST_URI"]=>
      string(28) "/~stejanse/tst.php?hahah=w12"
    }
    

    您可以使用这些变量来检索您的信息,而不是使用 mod_rewrite。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多