Apache 2.2 rewrite rule for / is not redirecting

346 Views Asked by At

I have config file in apache that should redirect / request to different url.

But i am getting 404 when i hit /

Request is going to backend server but it is not redirecting.

Config file is

#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]

Any idea?

1

There are 1 best solutions below

1
MrWhite On BEST ANSWER
#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

Because you are using a mod_alias RedirectMatch directive, which is processed after the other mod_rewrite (RewriteRule) directives despite the order of directives in the config file.

And/or you have ProxyPass directives that forward other requests? mod_rewrite is required to override this.

You need to use mod_rewrite RewriteRule here also in order to avoid the conflict. For example:

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

And this directive should be before the existing directives. (Although there shouldn't be a conflict in this case.) The L flag is required.

The P flag sends the request through mod_proxy - this isn't a "redirect".

The RedirectMatch directive you posted would default to a 302 (temporary) redirect, as I used here. However, if this is intended to be permanent then change to a 301, but only once you have confirmed that it works as intended (to avoid caching issues).