1

There is lots of information about redirecting a whole domain to another domain but I cannot find any answered questions about redirectly a domain to a specific page on a second domain. This is what I have set up:

<VirtualHost *:80>
    ServerName www.domain2.org.uk
    ServerAlias domain2.org.uk domain1.co.uk www.domain1.co.uk
    DocumentRoot /var/www/html
    ErrorLog logs/mkt-error_log
    CustomLog logs/mkt-access_log common
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !domain1.co.uk$ [NC]
    RewriteRule ^(.*)$ https://www.domain2.org.uk/specific-page/ [L,R=301]
</VirtualHost>

This does redirect but just to the default page for domain2. How can I change this so that it goes to the specific-page?

1
  • What do you mean by "the default page for domain2"? What redirect(s) are you seeing in the network traffic?
    – MrWhite
    Jul 21 '19 at 21:47
2
RewriteRule ^(.*)$ https://www.domain2.org.uk/specific-page/ [L,R=301]

This already does what you require and redirects everything to https://www.domain2.org.uk/specific-page/ - as stated in the RewriteRule substitution. (Although the RewriteRule pattern is unnecessarily capturing a back-reference.)

RewriteCond %{HTTP_HOST} !domain1.co.uk$ [NC]

HOWEVER (unless this is a typo in your exemplified code snippet), the condition (that checks that the Host header does not end with domain1.co.uk) will result in a redirect loop (domain2.org.uk is obviously not domain1.co.uk, so the condition is always satisfied). And the following RewriteRule redirects everything.

The CondPattern in this RewriteCond directive needs to be changed, by either:

  • Removing the ! (negation) prefix. (Although the trailing $ would still cause issues with a FQDN that ends in a dot.)
  • OR, change to !domain2\.org\.uk$

But... since you clearly have access to the server config and are working in a virtualhost context, you shouldn't really be using mod_rewrite for this. You should create a separate vhost, one for domain1, the other for domain2 and use a mod_alias RedirectMatch directive instead... less prone to error, more efficient.

For example:

<VirtualHost *:80>
    ServerName domain1.org.uk
    ServerAlias www.domain1.org.uk
    RedirectMatch 301 ^ https://www.domain2.org.uk/specific-page/
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.org.uk
    ServerAlias www.domain2.org.uk
    DocumentRoot /var/www/html
    ErrorLog logs/mkt-error_log
    CustomLog logs/mkt-access_log common
</VirtualHost>

NB: These vhosts are only for HTTP (port 80), so you will naturally need to repeat this redirect for HTTPS (port 443).

2
  • That almost works for me. Now www.domain1.org.uk redirects to www.domain2.org.uk/specific-page/ but domain1.org.uk redirects to www.domain2.org.uk. I don't really understand why that is. Could this be because I have Cloudfare and that is affecting it? That's how I get the switch from http to https to work for example. Thanks
    – williamsdb
    Jul 22 '19 at 7:11
  • "Could this be because I have Cloudfare" - quite possibly. Check what page rules you have defined. This certainly isn't caused by the directives posted above.
    – MrWhite
    Jul 22 '19 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.