大网数据广告活动
服务器运维问答

Redirect whole domain to single page on another domain in Apache Virtual Host

· 52CE 我爱测

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 *:…

问题原文

原帖链接 · williamsdb · CC BY-SA 4.0

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?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

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).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。