We tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.net/$1 [L,R=301,NC]
You said you "recently migrated a subdomain", however, the code you posted does not target a subdomain and conflicts with the example hostnames you described in the question?
This will redirect "only the home pahe" if you have put it in the wrong place in the .htaccess
file. eg. If you have placed it after an internal rewrite to a front-controller. This redirect would need to go at the very top of your root .htaccess
file.
without slowing down the website or overloading the server.
200 like-for-like URL redirects are insignificant in terms of performance/overhead. However, to completely minimise all impact these redirects might have then the blog
subdomain should point to a different area of the filesystem, which simplifies the redirect further. (In fact, you don't need to point the subdomain at any particular directory and instead perform the redirect directly in the server config / virtual host, not .htaccess
.)
Your "200 redirects" can then be performed with a single mod_alias Redirect
directive (in .htaccess
or <VirtualHost>
container). For example:
Redirect 301 / https://www.example.com/blog/
The Redirect
directive is prefix-matching and everything after the match is copied to the end of the target URL. So, for example, http(s)://blog.example.com/anything
is 301 redirected to https://www.example.com/blog/anything
.
Otherwise, if your subdomain and main domain point to the same place on the filesystem then use mod_rewrite to check the requested hostname, much like as you did initially (although not sure why you were checking for example.com
or www.example.com
when you should have been checking for the subdomain blog.example.com
).
For example, at the very top of the root .htaccess
file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
The NC
flag is superfluous since the regex .*
already matches everything.
Or, use the REQUEST_URI
server variable instead in the RewriteRule
directive and avoid the backreference. For example:
:
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
The REQUEST_URI
server variable already includes the slash prefix.
Test first with a 302 (temporary) redirect to avoid potential caching issues. And make sure you have cleared your browser cache before testing.