【问题标题】:How do I create rules in “.htaccess” for redirecting URLs?如何在“.htaccess”中创建用于重定向 URL 的规则?
【发布时间】:2017-11-05 05:21:20
【问题描述】:

我希望发生以下重定向

example.com --> www.example.com/abc
www.example.com --> www.example.com/abc
example.com/abc --> www.example.com/abc
example.com/anyRubbish --> www.example.com/abc

但我对 RedirectCond 和 ReWriteRule 和 RewriteBase、[L,R 301] 中的 'L' 和 'R 等之间的区别感到困惑。

我正在为 Apache 服务器编写规则。

【问题讨论】:

标签: apache .htaccess redirect


【解决方案1】:

您可以使用以下规则:

 RewriteEngine on
 RewriteCond ℅{HTTP_HOST} !^www\. [NC,OR]
 RewriteCond ℅{REQUEST_URI} !^/ABC
 RewriteRule ^ http://www.example.com/abc℅{REQUEST_URI} [L,R]

这里有一个简短的解释:

第一个条件 rewrite cond ℅{HTTP_HOST} !^www\. [OR] 检查 http 主机头字符串是否不以 www 开头。如果主机主机值为 example.com,则条件返回 true。 第二个条件RewriteCond ℅{REQUEST_URI} !^/ABC 测试 Uri 。检查 Uri 是否不是 /ABC 。 .如果 URL 是 example.com/foobar,则返回 true;如果 URL 是 example.com/ABC,则返回 false OR 标志告诉 mod-rewrite 执行条件之一,如果满足上述脚本中的条件之一,则应用规则。

根据条件应用RewriteRule RewriteRule ^ http://www.example.com/abc℅{REQUEST_URI} [L,R],这会将所有请求重写到www.example.com/ABC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多