htaccess将所有文件重定向到index.php,而不是特定的子目录

时间:2013-03-12 12:00:14

标签: .htaccess

我有一个网站,其中所有请求都应该发送到index.php,除了特定子目录中的那些,应该转到子目录/ index.php。

我希望将目录路径转换为查询字符串,以便在子目录索引页面中使用。

我对htaccess文件一点都不太了解,我确信这个问题已被回答了一百次,但我找不到要搜索的内容以获得答案。

所以 - 这就是我想在这些条件下发生的事情......

RULE 1
www.example.com/anything_or_nothing <goes to> /index.php
www.example.com/anything/anything_else <goes to> /index.php
RULE 2
www.example.com/subdirectory/ <goes to> /subdirectory/index.php
RULE 3
www.example.com/subdirectory/some_other_directory/ <goes to> /subdirectory/index.php?a=some_other_directory

这就是我目前所拥有的。它不起作用 - 至少,规则1工作,规则2工作(虽然我认为这仅仅是因为物理/子目录存在而且index.php是默认服务。)规则3没有工作

Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^/?timeline/
#RULE 1
RewriteRule ^.*$ ./index.php
#RULE 2
RewriteRule ^/sub.*$ /sub/index.php
#RULE 3
RewriteRule ^/sub/([^/\.]+)/?$ /sub/index.php?a=$1 [L,NC]

我无法在子目录中获取.htaccess文件,因此我希望规则都在根.htaccess文件中。

我从其他网站拼凑了这个.htaccess,所以我为我的无知程度道歉,但我不知道该去哪里......

提前谢谢。

附加信息&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;

我尝试过的其他事情。

RewriteRule ^/sub/([^/\.]+)/?$ /sub/index.php?a=$1 [L,NC]
RewriteRule ^/sub.*$ /sub/index.php
RewriteRule ^.*$ ./index.php

停止所有工作规则。

此:

RewriteRule ^.*$ ./index.php
RewriteRule ^/sub/([^/\.]+)/?$ /sub/index.php?a=$1 [L,NC]
RewriteRule ^/sub.*$ /sub/index.php

似乎表现得像原始的例子。

1 个答案:

答案 0 :(得分:0)

当你回答自己时,第二条规则是不必要的。

# Don't rewrite if file already exists to prevent rewriting images, scripts etc
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

# Rewrite sub/anything to sub/index.php?a=anything
RewriteRule ^sub/([^/]+)$ sub/index.php?a=$1 [L,NC]

# Fallback all unknown requests to index.php
RewriteRule .* index.php [L,NC]

警告提示:当您将不存在的文件重写为 index.php 时,此方法将完全隐藏所有自然404错误。

相关问题