【问题标题】:How to redirect all subpages using htaccess and exclude specific URLs?如何使用 htaccess 重定向所有子页面并排除特定 URL?
【发布时间】:2012-07-24 21:06:37
【问题描述】:

我正在尝试使用 htaccess 进行重定向,但我对 htaccess 和重定向知之甚少。我想做的是:

重定向访问所有子页面和目录,例如

http://mydomain.com/index.php
http://mydomain.com/directory/
http://mydomain.com/new/directory/

对我域上的特定 URL 说:

http://mydomain.com/one

我试着用

redirect /index.php http://mydomain.com/demo/

但我的问题是如何从该重定向中排除特定的 URL 和目录?如何制定规则以排除特定页面被重定向?

感谢任何帮助。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您的“选择工具”应该是 RewriteCond

    RewriteCond 指令不仅可用于过滤 URL,而且每个条件仅适用于下一个 RewriteRule 指令 - 但是,您可以“堆叠”RewriteCond 指令。

    例子:

    RewriteCond %{REQUEST_URI} !^/path/to/non/redirect/url.php$
    RewriteCond %{REQUEST_URI} !^/path/to/another/non/redirect/url.php$
    RewriteRule ^$ http://mydomain.com/one
    

    不过,这可以稍微简化一下:

    RewriteCond %{REQUEST_URI} !^(/path/to/non/redirect/url.php|/path/to/another/non/redirect/url.php)$
    RewriteRule /index.php http://mydomain.com/one
    

    Apache Mod_Rewrite Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2022-06-30
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多