skip to Main Content

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


  1. Fastest solution (though not the best one) by modifying the htaccess

    Not 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:

    <IfModule mod_rewrite.c>
    
      RewriteEngine On
      RewriteBase /
    
      RewriteCond %{HTTPS} =off [OR,NC]
      RewriteCond %{HTTP_HOST} !^www. [OR,NC]
      RewriteCond %{HTTP_HOST} !.com$ [NC]
      RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    
      # BEGIN WordPress
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      # END WordPress
    
    </IfModule>
    

    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.

    Login or Signup to reply.
  2. WordPress’ rewrite rules are placed in between # BEGIN WordPress and # END WordPress block:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    ...
    </IfModule>
    
    # END WordPress
    

    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:

    # BEGIN Redirect to another domain name
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} ^80$ [OR]
    RewriteCond %{HTTP_HOST} ^(www.)?(example.net|example.info|example.xyz) [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
    </IfModule>
    
    # END Redirect to another domain name
    
    # Other rules here, if any.
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    ...
    </IfModule>
    
    # END WordPress
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search