【问题标题】:htaccess redirect to parent directory - bad requesthtaccess 重定向到父目录 - 错误请求
【发布时间】:2012-09-16 05:36:34
【问题描述】:

我们有一个 htaccess 文件来处理几个文件夹的漂亮链接。一些例子:

http://test.com/basecamp/en/foobar -> ../init.php?lang=en&request=foobar&site=basecamp
http://test.com/ravili/en/foobar   -> ../init.php?lang=en&request=foobar&site=ravili
http://test.com/ravili/ar/foo/bar  -> ../init.php?lang=ar&request=foo/bar&site=ravili

目录结构

/webroot
   init.php
/webroot/basecamp
   .htaccess
/webroot/ravili
   .htaccess

但现在,我们正在尝试将文件夹移动到单独的域中,因此我们将使用 http://test-basecamp.com 而不是 http://test-basecamp.com

所以.htaccess 不再重定向到 webroot 中的 init.php,我们在错误日志中收到 400 Bad Request 和“Invalid URI in request GET /en/ HTTP/1.1”

htaccess 文件

Options +FollowSymlinks
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} !/(content|images|css|js|fonts|pdfs)/
RewriteRule /([^.]+\.(jpe?g|gif|bmp|png|css|js|eot|svg|ttf|ico|pdf))$ /$1 [NC,R,L]

RewriteCond %{REQUEST_URI} !(content|images|css|js|fonts|pdfs)/.*
RewriteRule !^[a-z]{2}/ /en/ [NC,L,R]

RewriteCond %{REQUEST_URI} !/(content|images|css|js|fonts|pdfs)/
RewriteRule ^([a-z]{2})/(.*)$ ../init.php?lang=$1&request=$2&site=basecamp [L,QSA]

那么我们该如何解决这个问题呢?

【问题讨论】:

  • 你不能重定向到webroot之外的文件夹!
  • 嗯,问题是如果我将 init.php 文件移动到 webroot 中,我会收到重定向循环错误。看到这个问题:stackoverflow.com/questions/12427371/…

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

由于您已经有了/ 的重写基础,因此您的规则目标的../ 部分会导致400 错误请求(因为它试图遍历文档根目录的父目录)。从 RewriteRule 中删除 ../

RewriteRule ^([a-z]{2})/(.*)$ /init.php?lang=$1&request=$2&site=basecamp [L,QSA]

【讨论】:

  • 但 .htaccess 文件位于 basecamp 文件夹中,因此 / 将引用 /webroot/basecamp。我试过了,得到了一个重定向循环。
  • @gAMBOOKa RewriteBase //webroot/RewriteBase /basecamp//webroot/basecamp/。你得到一个重写循环的原因是因为你没有修复我在上一个问题中建议的内容,特别是当 URI 为 /init.php?lang=etcetc 时,RewriteCond %{REQUEST_URI} !/init.php/始终为真。那里没有斜线。它永远是正确的,它总是会一遍又一遍地重写为/init.php
  • @gAMBOOKa / 是根据httpd.conf 设置的,而不是根据.htaccess 的位置设置的。 +1 给 Lin 一个好的答案!
  • @Jon Lin:这是仍在抛出重定向循环的更新规则:RewriteCond %{REQUEST_URI} !/(init.php|content|images|css|js|fonts|pdfs)/ RewriteRule ^([a-z]{2})/(.*)$ /init.php?lang=$1&request=$2&site=basecamp [L,QSA]
  • @alfasin:在 httpd.conf 中,test-basecamp.com 的 webroot 是 /webroot/basecamp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多