I have a problem where I need to redirect my domain before it hits php. By the time it hits the ability to execute a header, it is too late.
How can I do
if (domain == 'www.example.com')
redirect www.domain.com;
in my .htaccess?
I have a problem where I need to redirect my domain before it hits php. By the time it hits the ability to execute a header, it is too late.
How can I do
if (domain == 'www.example.com')
redirect www.domain.com;
in my .htaccess?
Using mod_rewrite
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R,L]
^/?(.*)$
instead, to match out the leading slash if there is one, in case it gets used elsewhere.
As of Apache 2.4 you can use an <If>
Directive to achieve what you suggested:
<If "req('Host') == 'www.example.com'">
RedirectMatch (.*) http://www.example2.com$1
</If>
For a case-insensitive version that matches with or without the www you can do:
<If "req('Host') =~ /example.com/i">
RedirectMatch (.*) http://www.example2.com$1
</If>
Sources:
RewriteEngine On <If "req('Host') =~ /example.com/i"> RedirectMatch 301 ^(.*) https://www.example2.com$1 </If>
May 6, 2019 at 18:25