peeDee
peeDee

Reputation: 65

Apache host with multiple alias/domains rewrite rule

I have an apache vhost with a server and multiple aliases. The server being the production/live domain and the aliases being the dev and staging servers. For example:

ServerName foo.bar
ServerAlias dev.superfoo.bar
ServerAlias stage.superfoo.bar

Is there a rewrite rule that would preserve the domain, whatever it may be, and just redirect people from domain/en-us/anything to domain/en/anything?

I believe that ^/en-us/(.*)$ foo.bar/en/1$ would do the trick for one domain. Is there a way to preserve the domain instead of writing the rule for every alias?

Upvotes: 1

Views: 930

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270757

To ensure the correct domain is retained on each one, you may use the %{HTTP_HOST} variable in the redirection:

RewriteEngine On
RewriteRule ^/en-us/(.*)$ http://%{HTTP_HOST}/en/$1 [L,R=301]

The [R=301] flag issues a permanent 301 redirect, but in testing you may want to use 302 since browsers might aggressively cache the redirect making it hard to debug.

Above I used the leading / as in ^/en-us. That will be necessary if you use this rule at the VirtualHost level. However, if you use it inside a .htaccess file in your document root, you must not include the /, as in:

RewriteEngine On
RewriteRule ^en-us/(.*)$ http://%{HTTP_HOST}/en/$1 [L,R=301]

The rules around that are noted in the "what is matched" section of the RewriteRule manual.

Upvotes: 1

Related Questions