Forum Moderators: phranque

Message Too Old, No Replies

need help with rewrite config (multiple domains into single vhost)

         

Budrumi

10:08 am on Aug 21, 2014 (gmt 0)

10+ Year Member



Hi. I'm no Apache expert and I'd appreciate your help with the following.

I have an Apache server that has 2 vhosts (dev.example.com and www.example.com). I have plenty of national commercial domain names registered (like example.co.uk, example.eu etc.).

There was a request to redirect all those domains to www.example.com (including the domain names with and without www prefix). I've achieved that using ServerAlias directive in the www.example.com vhost.

Now there's also a requirement to not only redirect, but also rewrite all those URLs to www.example.com.

Can anyone help me with what the rewrite module configuration should look like?

I've found this post at serverfault (http://stackoverflow.com/questions/5966827/redirecting-multiple-domains-urls-to-one-canonical-domain), but when I try that, all those domains then lead to the dev.example.com site (I can tell, because that site requires authentication). I need to leave the dev.example.com intact and accessible separately (without any redirection/rewrite).

Thanks in advance

Budrumi

10:13 am on Aug 21, 2014 (gmt 0)

10+ Year Member



I should also add that there's 3rd vhost for HTTPS access to www.example.com. The rewrite config should ofcourse rewrite both HTTP and HTTPS requests.

penders

11:04 am on Aug 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now there's also a requirement to not only redirect, but also rewrite all those URLs to www.example.com.


What do you mean by this? You can't (externally) redirect AND (internally) rewrite the request - to the same URL?

Budrumi

12:56 pm on Aug 21, 2014 (gmt 0)

10+ Year Member



Sorry, might be my incorrect terminology.
I meant that as of now with ServerAlias usage, when I enter www.example.co.uk in browser, it's redirected to www.example.com vhost content in Apache, but the url in browser stays www.example.co.uk/{whatever}.
The requirement is to rewrite the URL, so when I type www.example.co.uk in the browser, it's "redirected/rewritten" to www.example.com and it also reflects that in the URL in browser.

Am I making more sense now?

lucy24

3:16 pm on Aug 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it's redirected to www.example.com vhost content in Apache, but the url in browser stays

Then it's not being redirected. It's being rewritten, whether by a RewriteRule or a server alias or some other means.

"redirect" = tell the browser to make a fresh request for some other URL, either on the same or a different host. (The same word applies to both.)

From the user's point of view: If the browser's address bar changes, it's a redirect. If not, it's a rewrite.

If you're doing it via mod_rewrite and you need to exclude some particular hostname but include all others, you'll need a RewriteCond that says something like

RewriteCond %{HTTP_HOST} !(dev|rightname)\.example\.com


where "dev" is dev (duhh) and "rightname" means the hostname you're redirecting to, so as to avoid infinite loops.

Budrumi

10:10 pm on Aug 21, 2014 (gmt 0)

10+ Year Member



Alright, thanks for the terms clarification.
What would the RewriteRule look like then?
I've tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !(dev|www)\.example\.com
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]

and commented out the ServerAlias portion in the www.example.com vhost, restarted Apache and tried browsing to for example example.co.uk or www.example.eu, but it always brings me to the dev.example.com site.

lucy24

12:11 am on Aug 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That doesn't sound like a redirect issue. It sounds like a server alias and/or DNS issue :(

Budrumi

9:35 am on Aug 22, 2014 (gmt 0)

10+ Year Member



I don't think this is a DNS issue. All zones in the DNS contain two A records (example.com and www.example.com), both leading to the same IP of the Apache server.

I think the rewrite configuration didn't work/get processed at all. I had the "dev" vhost as first entry in httpd-vhosts.conf, so anything not evaluated by the two defined vhosts always went to this first one. I moved the www.example.com vhosts to be the first in the config file and now it always goes to the www.example.com site, but the URL isn't rewritten.

I've put the rewrite config in httpd.conf. Should I put it elsewhere like in the <VirtualHost> section itself?