1

我有一个站点,其中所有请求都应该转到 index.php,除了特定子目录中的那些应该转到 subdirectory/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,所以我为我的无知程度道歉,但我只是不知道该去哪里......

先感谢您。

附加信息>>>>>>>>

我尝试过的其他事情。

这个

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

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

4

1 回答 1

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 错误。

于 2013-03-12T13:11:48.100 回答