【发布时间】: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.php到index.phpRewriteRule。 -
@Prix 你能提供任何样品吗?我不是很理解。
-
@Justin 这是一个非常简单的示例pastebin.com/vR7zLwD8,如果您的应用程序在子目录上运行,这就是它的样子,正如您所看到的,您真正改变的只是
RewriteBase和index.php检查。 如果这对您的情况有用,我会将其转换为答案,但如果您不想更改任何文件或任何内容,我会将其作为评论留下。
标签: php .htaccess mod-rewrite redirect