【问题标题】:htaccess redirects unexpectedly to folderhtaccess 意外重定向到文件夹
【发布时间】:2011-09-26 16:06:20
【问题描述】:

我有一个奇怪的问题。我正在开发 Zend Framework 应用程序并具有以下文件结构:

/
/application/
/public/
/public/.htaccess
/public/index.php
/public/js/
/public/style/
/public/profile/
/.htaccess

我的域名指向folder /

当我输入地址example.com/profile/ 时,它会进入控制器配置文件,这很好。 当我输入地址example.com/profile 时,服务器会将我重定向到: example.com/public/profile/

我希望有一个解决方案,只要我请求: example.com/profile/example.com/profile

将呈现相同的页面,但第二个版本给了我一个重定向,我不知道为什么。

/.htaccess 文件是:

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

此文件的作用是将所有流量从 / 路由到 /public 但不进行任何重定向。

/public/.htaccess 文件是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

谁能帮我解决这个问题?谢谢。


我解决了这个问题。如果有人遇到同样的问题,这里是解决方案: 要解决此问题,您必须设置以下选项并禁用 DirectorySlash

Options -Indexes FollowSymLinks
DirectorySlash Off

现在,当您指向目录时,Apache 不应在 uri 末尾添加斜杠。 这也禁用了重定向到带有斜杠的 uri。

【问题讨论】:

  • 为什么不在.htaccess的第一个版本中尝试RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l RewriteRule ^(.*)$ public/index.php [L]
  • 那么我在 apache 日志中有错误:请求超出了 10 个内部重定向的限制
  • 我在外部 htaccess 中添加了以下代码:RewriteEngine On RewriteRule ^(.*)$ public/$1 RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 现在我有内部服务器错误,因为内部重定向太多...
  • 我修复了这个奇怪的行为。我所做的是从 dir_module 禁用 DirectorySlash: DirectorySlash Off
  • 将您的答案添加为答案之一并将其标记为已接受。因此,如果有人试图查看您的问题,他们可以看到“绿色”颜色标记;

标签: php apache zend-framework .htaccess redirect


【解决方案1】:

可以选择匹配最后一个/。将您的 .htaccess 文件更改为:

RewriteEngine On
RewriteRule ^(.*)/?$ public/$1 [L]

【讨论】:

  • 不工作。我尝试:example.com/profil 并将我重定向到 example.com/public/profil 当我尝试 example.com/profil 时一切正常。
  • 对不起。我在这条评论中有错误。当我尝试:example.com/profil 时,它会重定向到 example.com/public/profil/
  • 有人有解决这个问题的办法吗?
【解决方案2】:

这对我有用:

RewriteEngine On
RewriteRule ^/$|^(.*)$  /public/$1 [QSA,NC,L]

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 2020-05-17
    • 2014-08-04
    • 2016-08-30
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多