【问题标题】:.htaccess redirect to index.php in any directory.htaccess 重定向到任何目录中的 index.php
【发布时间】:2014-10-23 19:26:39
【问题描述】:

我一直在研究我在 php 中创建的框架,它工作的重要部分是将所有 url 查询发送到根文件夹中的 index.php。 我设法在我的 .htaccess 文件中的以下代码中做到了这一点

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
#RewriteRule ^(phpmyadmin)($|/) - [L]
RewriteRule ^(html)($|/) - [L]
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php

例如,当我将网站移动到不同的文件夹时会出现问题

www.mydomain.com/subdir/{where the index.php is, along with htaccess}

它在这里停止工作。如果我也将框架应用程序移动到子域,也会发生同样的情况。

所以我正在尝试修改 .htaccess 以在 .htaccess 文件所在的位置正确地重写为 index.php,无论它是在子目录还是子域中。我怎样才能让 .htaccess 知道应用程序位于子目录中并正确重写它,这样位置的更改不会破坏 .htaccess 指向正确的文件?

 www.domain.com/{.htaccess+index.php + framework} => works properly
 www.subdom.domain.com/{.htaccess+index.php + framework}  => does not work
 www.domain.com/subdir/{.htaccess+index.php + framework} => does not work //send to www.domain.com/index.php
 localhost/projectname/{.htaccess+index.php + framework} =>does not work

如您所见,它需要将请求发送到 htaccess 所在的 index.php。

【问题讨论】:

  • /index.php 更改为 index.php?当然,现在您需要在每个目录中都有一个 index.php。
  • 您不适合使用RewriteBase 吗?有了它,当您需要移动时,您可以通过更改 .htaccess 上的 2 个条目来轻松解决问题。 RewriteBase /folder_it_is_now/ 和你的 /index.phpindex.php RewriteRule
  • @Prix 你能提供任何样品吗?我不是很理解。
  • @Justin 这是一个非常简单的示例pastebin.com/vR7zLwD8,如果您的应用程序在子目录上运行,这就是它的样子,正如您所看到的,您真正改变的只是RewriteBaseindex.php 检查。 如果这对您的情况有用,我会将其转换为答案,但如果您不想更改任何文件或任何内容,我会将其作为评论留下。

标签: php .htaccess mod-rewrite redirect


【解决方案1】:

根据新示例进行编辑:

这样的事情怎么样?基本上检查以确保它不是文件,如果不是文件重定向到 index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php [PT,L,QSA]

【讨论】:

  • 我认为这可以解决问题,但我正在尝试通过 .htaccess 自行解决。
  • 除非您有其他特殊情况,否则 apache 无法知道新的子目录在哪里。您是否有尝试使用的 URL 示例?他们是否遵循一种特殊的模式?
  • 我刚刚添加了示例网址供您参考。请看一看。
【解决方案2】:

你可以使用这个技巧来一直写入当前目录的index.php:

RewriteEngine On

# Generate BASE dynamically
# It compares REQUEST_URI variable (which is complete path) with the URI matched by 
# RewriteRule (which is relative to current path) and gets differential in
$ %{ENV:BASE} variable.
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

RewriteRule ^(html)($|/) - [L]

# Redirect requests to %{ENV:BASE}index.php which is index.php of current directory
# It also makes sure index.php exists in current directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.php -f [NC]
RewriteRule . %{ENV:BASE}index.php [L]

【讨论】:

  • +1 非常有趣的ENV,您不需要对照%{DOCUMENT_ROOT} 来确定@​​987654324@ 的存在吗?
  • @Prix:谢谢,是的,确保index.php 存在是一个很好的检查。我会在我的答案中添加。
  • 另外,我认为如果您可以扩展对第一条规则发生的情况的解释会非常好,因为它将循环查找正确的基础,而最后一条规则将在索引时停止。 php 找到了。
  • 这在我在本地主机上测试时有效。让我在真机上试一下,看看有没有问题,大家可以根据需要使用。
  • @anubhava 这解决了我的应用程序位置问题。你介意给我一个链接,看看我可以在哪里了解你的答案方法吗?
猜你喜欢
  • 2017-01-12
  • 2012-04-27
  • 1970-01-01
  • 2013-03-06
  • 2016-11-30
  • 2016-11-22
  • 2016-01-21
  • 2014-05-14
  • 2019-11-29
相关资源
最近更新 更多