3
votes

I have an apache config file making use of both mod_alias's Redirect directive, and mod_rewrite's RewriteRule.

From my understanding, mod_alias works in order of top to bottom of the file, and mod_rewrite the opposite, the last rule is matched, unless you terminate with the [L] flag.

My question is, my Redirect rules are placed above the RewriteRule, yet the RewriteRule still seems to be being applied, so I end up redirecting everyone to the same url.

What I am trying to achieve is redirect certain locations to another webserver, and have a catch-all with mod-rewrite so a snippet looks like this

<VirtualHost *:80>
ServerName example.com
Redirect permanent "/foo/bar/location1/" "https://example2.com/foo/baz/location1"
Redirect permanent "/foo/bar/location2/abc" "https://example2.com/foo/baz/location2/def"

RewriteEngine on
RewriteRule ^.*$ "https://example2.com/catch-all/? [R=301, L]

Also, do these behave differently if they are inside a block or outside?

1

1 Answers

2
votes

You have to write Redirect rule after Rewrite rule as it will first check what is the rewrite rule it has to follow and then the redirect rules.

<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteRule ^.*$ "https://example2.com/catch-all/? [R=301, L]
Redirect permanent "/foo/bar/location1/" "https://example2.com/foo/baz/location1"
Redirect permanent "/foo/bar/location2/abc" "https://example2.com/foo/baz/location2/def"