【问题标题】:htaccess to redirect all files to index.php apart from a specific subdirectoryhtaccess 将所有文件重定向到 index.php 除了特定的子目录
【发布时间】:2013-03-12 12:00:13
【问题描述】:

我有一个站点,其中所有请求都应该转到 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

似乎表现得和原来的例子一样。

【问题讨论】:

  • 您是否尝试更改 RewriteRules 的顺序?例如反转它。
  • 感谢您的回复-我已经尝试过-在上面的帖子中进行编辑。

标签: .htaccess


【解决方案1】:

正如您自己回答的那样,第二条规则是不必要的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2016-11-30
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多