I’ve got a domain name with three different Top-Level-Domain (TLD), using WordPress and hosted on OVH.
I’d like to redirect each of them to the .com
one, using the HTTPS protocol and the www
subdomain.
I usually use the following code sample to redirect my websites to https://www.example.com
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The problem:
Since I’m using WordPress, there already is a redirection. Furthermore, I’ve node idea how to properly combine the TLD redirection to the https://www one.
Here is the .htaccess code from WordPress.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2
Answers
Fastest solution
(though not the best one)by modifying the htaccessNot the best one because WP can modify/overwrite the htaccess.This is not true anymore, since WP uses this function to only rewrite its own part of the htaccess (between# BEGIN WordPress
and# END WordPress
). Simply add your rules before WP’s main rule. Here is an example combining all conditions in one rule:Plugin solution without touching the htaccess
You could use a plugin to do that properly. You can find plenty of them by searching on the web.
WordPress’ rewrite rules are placed in between
# BEGIN WordPress
and# END WordPress
block:And WordPress uses its
insert_with_markers()
function to make sure WordPress is only altering its own rewrite rules.So WordPress wouldn’t alter any rewrite rules outside of that block, and therefore you could safely add yours in the
.htaccess
file.Now to redirect from a domain to another domain, the following worked well for me: