【问题标题】:Rewriting all request to index.php in the same diectory将所有请求重写到同一目录中的 index.php
【发布时间】:2014-03-01 00:05:49
【问题描述】:

我希望将所有请求重写为 index.php,所以我使用了这个 htaccess 代码。

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

当站点托管在主目录上时,它可以正常工作。但是假设如果我将我的站点移动到子目录xyz 并将我的所有文件(包括 .htacess 文件)移动到该目录中。然后如果我访问http://example.com/xyz/some_page,请求不会重定向到/xyz/index.php

那么,我如何才能像我的情况一样使这种重写工作甚至在子目录上工作。

更新: 我忘了提到目录xyz,因为在我的情况下可能会经常更改。所以,这个目录不需要在重写规则中硬编码

【问题讨论】:

  • @AD7six 抱歉,我正在使用更新后的 .htaccess 规则。我之前发错了
  • @AD7six 您提供的链接对我有用。你能解释一下这两行之间的区别吗? RewriteRule ^ index.php [L]RewriteRule . index.php [L]。顺便说一句,谢谢你的回答
  • 它们基本相同。第一个匹配 url 的开头(不管 url 是什么)并重写为 index.php;第二个匹配 url 的第一个字符(无论该字符是什么 - 它将是 '/')并重写为 index.php。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

移除 RewriteBase

如果您不需要重写基础 - 只需将其删除。基于问题中显示的示例的工作示例是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

请注意,第一个重写规则已被删除,因为它在逻辑上等同于RewriteCond %{REQUEST_FILENAME} !-f(不要重写对已存在文件的请求)。

考虑同时删除RewriteCond %{REQUEST_FILENAME} !-d,因为这样可以防止将/this/folder/exists 之类的网址发送到index.php。如果Directory listings are enabled 最相关但不需要。

【讨论】:

【解决方案2】:

更改您的基本网址以进行重写

RewriteBase subdir/

或尝试使用完整的网址

RewriteBase http://www.examle.com/subdir/

【讨论】:

  • 对不起,我忘了提到 xyz 目录,因为在我的情况下可能会经常更改。所以,这个目录不需要在重写规则中硬编码
猜你喜欢
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2014-07-08
  • 2013-01-01
  • 1970-01-01
  • 2019-04-27
  • 2016-11-30
相关资源
最近更新 更多