简体   繁体   中英

mod_rewrite Redirect to specific URL if domain X is used?

I have a site with two domains. I want that whenever domain X is visited (no matter what the path is... /foo/bar/, root or whatever), the browser should redirect to a specific URL.

So:

domainX.com -> domainX.com
domainY.com -> domainX.com/some/path

The following kinda sorta works, but it only matches against domainY.com , so www.domainY.com or domainY.com/some/path doesn't work.

RewriteCond %{HTTP_HOST} ^domainY\.com
RewriteRule ^(.*)$ http://domainX\.com/some/path [L]

It has to accept both with and without www before though. Any ideas?

You can extend the regular expression for HTTP_HOST

RewriteCond %{HTTP_HOST} ^(?:www\.)?domainY\.com$
RewriteRule .* http://domainX.com/some/path [L]

This is a rewrite. If you want to redirect the client, you must add an R flag

RewriteRule .* http://domainX.com/some/path [R,L]

When everything works as it should, you may replace R with R=301 . Never test with R=301 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM