【问题标题】:How can I redirect all sub-directories to root using htaccess or httpd.conf?如何使用 htaccess 或 httpd.conf 将所有子目录重定向到 root?
【发布时间】:2011-10-16 05:37:37
【问题描述】:

我有一个基于 n PATH_INFO 变量构建内容的索引文件。

例子:

site.com/A/B/n/

应该在以下任一位置使用 index.php:

site.com/index.php?var1=A&var2=B&varN=n
 - or - 
site.com/index.php/A/B/n/

代替:

site.com/A/B/n/index.php || which doesn't exist ||

到目前为止,我已经尝试了多种变体:

RedirectMatch ^/.+/.*$ /

没有成功。

我在这里有一个不优雅且不可扩展的解决方案:

RewriteEngine On 
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]

此解决方案的问题:

  • 不优雅且不可扩展,每个子目录都需要手动行
  • 使用非字母数字字符(主要是 +、= 和 &)例如失败。 site.com/ab&c/de+f/ (请注意,即使将正则表达式更改为 ^([a-zA-Z0-9_-\+\=\&]+)/?$ 也无济于事,实际上完全出错了)

你能帮忙吗?

【问题讨论】:

  • 那么..你更喜欢哪个:site.com/index.php?var1=A&var2=B&varN=n(需要多个规则)或site.com/index.php/A/B/n/(可以使用单个规则)?
  • 我都愿意。在平等的情况下,我更喜欢一条线。

标签: .htaccess mod-rewrite apache apache-config


【解决方案1】:

添加以下内容会将所有流量(对于不存在的文件)重定向到索引页面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]

然后,您可以通过解析“REQUEST_URI”在 index.php 中做出决定

【讨论】:

  • 你能告诉我前两行有什么用吗?
  • @PushkerYadav 如果请求文件名不同于 -f(文件)或 -d(目录)
【解决方案2】:

选项 1: (site.com/index.php?var1=A&var2=B&varN=n):

Options +FollowSymLinks -MultiViews
RewriteEngine On

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteRule ^([^/]+)/?$ index.php?p1=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]

1。你有[NC] 标志......所以你的模式中不需要有A-Z

2。而不是[a-zA-Z0-9_-\+\=\&][a-zA-Z0-9_-] 我使用[^/] 这意味着除斜杠/之外的任何字符。

3。添加了[QSA] 标志以保留现有的查询字符串。

选项 2: (site.com/index.php/A/B/n/):

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]

实际上,如果您不打算在任何地方显示该 URL(例如,301 重定向等),最后一行可以很容易地替换为 RewriteRule .* index.php [L] -- 您将在 PHP 中使用 $_SERVER['REQUEST_URI'] 查找原始 URL无论如何都要代码。

【讨论】:

  • 超越,LazyOne。这是一件美丽的事情。它甚至可以解决我读过很多关于加号 mod_rewrite 的问题。伟大的工作。
  • rott目录下有个与url同名的子目录怎么办?假设我希望访问“www.domain.com/preferences”,但我有偏好文件夹
  • @Tal 您必须将其添加到“排除列表”(附加条件),因为现在它将/应该将其视为RewriteCond %{REQUEST_FILENAME} !-d 部分对现有文件夹的访问
  • @LazyOne 我如何创建这个条件/排除列表?
  • @Tal 您最好创建单独的问题来描述您的情况。现在我无法访问任何 Apache 服务器来验证我的想法(我需要它,因为我已经有几个月没有使用 Apache 并且不想给你错误的建议 - 没有回答这样的 htaccess/Apache一年多的问题 - 需要一些测试环境来检查我的想法/想法之前回答)。
【解决方案3】:

重写规则 ^/.+ / [R=301,L]

任何路径超出 root 的任何 url 都将被重定向到 root。

^ = start of url
/ = a slash
.+ = 1 or more characters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2023-04-10
    • 2016-05-02
    • 2016-02-19
    • 2023-03-27
    相关资源
    最近更新 更多