RedirectMatch覆盖.htaccess中的Redirect

p4rjhz4m  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(182)

在我的.htaccess文件中,我有重定向的规则。

Redirect 301 /abc/ newdomain.example/abc-new/ 

Redirect 301 /def/ newdomain.example/def-new/

Redirect 301 /ghi/ newdomain.example/ghi-new/

RedirectMatch 301 ^/ newdomain.example

我的期望是:
1.当某人键入olddomain.example/abc时,被重定向到newdomain.example/abc-new/olddomain.example/def被重定向到newdomain.example/def-new/
1.当某人键入例如olddomain.example/xyz时,被重定向到newdomain.example
在上面的示例中,所有页面(如olddomain.example/abcolddomain.example/def)都被重定向到newdomain.example
删除带有RedirectMatch的行后,重定向工作。
我期望根据文件规则,文件中的规则从上到下进行分析,当匹配的东西被拿起。这里看起来RedirectMatch覆盖了上面的规则。

eanckbw9

eanckbw91#

在这种情况下,不需要使用RedirectMatchRedirect规则都是“以”规则开始。因此,您可以将其简化为:

Redirect 301 /abc https://newdomain.example/abc-new 
Redirect 301 /def https://newdomain.example/def-new
Redirect 301 /ghi https://newdomain.example/ghi-new
Redirect 301 / https://newdomain.example/

我在本地服务器上测试了这个:

$ curl -s --head https://olddomain.example/foo| grep Location
Location: https://newdomain.example/
$ curl -s --head https://olddomain.example | grep Location
Location: https://newdomain.example/
$ curl -s --head https://olddomain.example/ | grep Location
Location: https://newdomain.example/
$ curl -s --head https://olddomain.example/abc | grep Location
Location: https://newdomain.example/abc-new
$ curl -s --head https://olddomain.example/abc/ | grep Location
Location: https://newdomain.example/abc-new/
$ curl -s --head https://olddomain.example/abc/path | grep Location
Location: https://newdomain.example/abc-new/path
$ curl -s --head https://olddomain.example/xyz | grep Location
Location: https://newdomain.example/xyz

如果你真的想将其他内容重定向到主页(不推荐),你可以使用以下方法:

Redirect 301 /abc https://newdomain.example/abc-new 
Redirect 301 /def https://newdomain.example/def-new
Redirect 301 /ghi https://newdomain.example/ghi-new
RedirectMatch 301 /.* https://newdomain.example/

在我的本地服务器上测试:

$ curl -s --head https://olddomain.example | grep Location
Location: https://newdomain.example/
$ curl -s --head https://olddomain.example/ | grep Location
Location: https://newdomain.example/
$ curl -s --head https://olddomain.example/abc | grep Location
Location: https://newdomain.example/abc-new
$ curl -s --head https://olddomain.example/abc/ | grep Location
Location: https://newdomain.example/abc-new/
$ curl -s --head https://olddomain.example/abc/path | grep Location
Location: https://newdomain.example/abc-new/path
$ curl -s --head https://olddomain.example/xyz | grep Location
Location: https://newdomain.example/

相关问题