首页 > 解决方案 > / 的 Apache 2.2 重写规则未重定向

问题描述

我在 apache 中有配置文件,应该将/请求重定向到不同的 url。

但是当我点击/时我得到 404

请求将发送到后端服务器,但它没有重定向。

配置文件是

#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html

RewriteCond %{REQUEST_URI} ^/folder1/folder2
RewriteCond %{REQUEST_URI} !^/(.*)\.(json|html)
RewriteRule ^/(.*)$ http://app2.adobecqms.net/$1.html [P]

RewriteCond %{REQUEST_URI} ^/analytics.txt
RewriteRule ^/(.*)$ http://app2.adobecqms.net/page1/page2/page3/$1 [P]

#I also tried with below. But no luck
#This is returning 502
#RewriteRule ^/$ https://app1.abc.com/page1/page2.html [P]

任何想法?

标签: mod-rewriteapache2.2

解决方案


#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html

因为您使用的是 mod_aliasRedirectMatch指令,它在其他 mod_rewrite ( ) 指令之后RewriteRule进行处理,尽管配置文件中的指令顺序不同。

和/或您有ProxyPass转发其他请求的指令?需要 mod_rewrite 来覆盖它。

您还需要在RewriteRule此处使用 mod_rewrite 以避免冲突。例如:

RewriteRule ^/$ https://app1.abc.com/page1/page2.html [R=302,L]

而且这个指令应该现有指令之前。(尽管在这种情况下不应该有冲突。)L标志是必需的。

P标志通过 mod_proxy 发送请求 - 这不是“重定向”。

您发布的RedirectMatch指令将默认为 302(临时)重定向,正如我在此处使用的那样。但是,如果这是永久性的,则更改为 301,但前提是您确认它按预期工作(以避免缓存问题)。


推荐阅读