【问题标题】:Why does this RewriteRule work with [R] but not with [QSA,L]?为什么这个 RewriteRule 可以与 [R] 一起使用,但不能与 [QSA,L] 一起使用?
【发布时间】:2014-02-13 06:43:49
【问题描述】:

我有一个非常简单的 RewriteRule,但我不知道为什么它不想工作。也许只是说累了,但有些东西似乎很不稳定。

我想将一个看起来像 http://mydomain.com/abc 的 URL 重定向到 http://mydomain.com/abc/index.php/xyz,但我不想更改 URL。

这是我的 .htaccess 的全部内容:

RewriteEngine On 
RewriteBase /abc/

#redirect the homepage 
RewriteRule ^$  index.php/msj [QSA,L] 

当我这样做时,我得到“没有指定输入文件。”。如果我将 [QSA,L] 更改为 [R] 它可以工作,但它实际上会重定向 URL。

我误会了什么?

编辑:$_SERVER 的输出

array(34) { [
"PATH"]=> string(29) "/bin:/usr/bin:/sbin:/usr/sbin" [
"RAILS_ENV"]=> string(10) "production" [
"FCGI_ROLE"]=> string(9) "RESPONDER" [
"UNIQUE_ID"]=> string(24) "UxDcvK3suH0AABgWvXUAAAAj" [
"SCRIPT_URL"]=> string(1) "/" [
"SCRIPT_URI"]=> string(22) "http://themspress.org/" [
"dsid"]=> string(8) "25793844" [
"ds_id_25793844"]=> string(0) "" [
"DH_USER"]=> string(13) "juancommander" [
"HTTP_HOST"]=> string(14) "themspress.org" [
"HTTP_CONNECTION"]=> string(5) "close" [
"HTTP_ACCEPT"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" [
"HTTP_USER_AGENT"]=> string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36" [
"HTTP_ACCEPT_ENCODING"]=> string(17) "gzip,deflate,sdch" [
"HTTP_ACCEPT_LANGUAGE"]=> string(23) "en-US,en;q=0.8,es;q=0.6" [
"HTTP_COOKIE"]=> string(190) "OJSSID=Siy7xdofJurGtBcNUk1880; __utma=154159997.1153519437.1393351515.1393522373.1393598726.4; __utmc=154159997; __utmz=154159997.1393351515.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)" [
"SERVER_SIGNATURE"]=> string(0) "" [
"SERVER_SOFTWARE"]=> string(6) "Apache" [
"SERVER_NAME"]=> string(14) "themspress.org" [
"SERVER_ADDR"]=> string(15) "173.236.187.201" [
"SERVER_PORT"]=> string(2) "80" [
"REMOTE_ADDR"]=> string(14) "189.138.120.63" [
"DOCUMENT_ROOT"]=> string(51) "/home/juancommander/themspress.org/var/www/html/ojs" [
"SERVER_ADMIN"]=> string(24) "webmaster@themspress.org" [
"SCRIPT_FILENAME"]=> string(61) "/home/juancommander/themspress.org/var/www/html/ojs/index.php" [
"REMOTE_PORT"]=> string(5) "53719" [
"GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" [
"SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" [
"REQUEST_METHOD"]=> string(3) "GET" [
"QUERY_STRING"]=> string(0) "" [
"REQUEST_URI"]=> string(1) "/" [
"SCRIPT_NAME"]=> string(10) "/index.php" [
"PHP_SELF"]=> string(10) "/index.php" [
"REQUEST_TIME"]=> int(1393614012) }

【问题讨论】:

  • 这是 CI 还是其他框架?
  • 它的开放期刊系统。它使用 PHP。实际上,我在另一台具有相同设置的服务器上安装了另一个(尽管显然不完全相同)并且它在那里工作......
  • abc 是真实的目录吗?您的 htaccess 文件位于哪个目录?
  • 如果你想传递到 php 处理程序,你可能需要在RewriteRule 上使用PT flag
  • abc 是一个真实的目录,.htaccess 文件就在那个目录里面。

标签: apache .htaccess mod-rewrite


【解决方案1】:

我在Dreamhost documentation 中找到了问题的根源。

看来:

FastCGI 版本与 Apache 2.2 配对时似乎不喜欢 重写 index.php/$1。相反,它更喜欢 index.php?$1 但有些 CMS 不喜欢这样。

我使用的 CMS 不喜欢这样。所以撤消使用 ?$1 的评论在正确的轨道上,但是因为我没有解释,而且我使用的 CMS(开放期刊系统)在 ? 之后不能与 pathinfo 的东西一起工作,所以它就是不工作。

解决方案是从 FastCGI 更改为常规 CGI。

【讨论】:

    【解决方案2】:

    会发生什么:

    您发送http://mydomain.com/abc 的请求。 Apache 的 mod_rewrite 将 URL 重写为 http://mydomain.com/abc/index.php/msj 但保留了第一个 URI。因此,当 Apache 要求 php 处理请求时,$_SERVER['REQUEST_URI'] 将是 /abc/ 而不是 /abc/index.php/msj 并且由于您的 php 应用程序(OJS)使用 $_SERVER['REQUEST_URI'] 来处理请求,它不会知道应用到的重写你的网址。当您将[QSA,L] 替换为[R] 时,apache 会向您的浏览器发送一个Location 标头。您的浏览器发送另一个实际更改 $_SERVER['REQUEST_URI'] 的请求。

    不幸的是,没有办法我知道重写那部分。


    更新: 把它放在你的.htaccess 文件中并检查它是否有效?

    AcceptPathInfo On
    

    诡计

    把它放在你的.htaccess 文件中:

    RewriteRule ^$  index.php?undone=/msj [QSA,L] 
    

    编辑您的index.php 文件并将其放在最开头:

        if(isset($_GET['undone'])){
            $_SERVER['REQUEST_URI'] = '/abc/index.php' . $_GET['undone'];
            unset($_GET['undone']);
    
        }
    

    【讨论】:

    • 好的,这是一个好的开始。但我在另一台服务器上有完全相同的重写规则,并且有效......没有关于如何修复的建议?
    • @pocketfullofcheese 另一个服务器?那是 nginx 吗?
    • 什么意思,多余的?两者都是 apache。
    • 编辑您的 index.php 并将其放在文件的开头:var_dump($_SERVER);exit;。将输出放在您的问题中!
    • 将您的重写规则编辑为RewriteRule ^$ index.php/msj [QSA,L,E=PATH_INFO:/msj] 。测试它并告诉我会发生什么!
    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2015-02-27
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多