Search code examples
.htaccesshttp-redirect

htaccess non-www does not redirect


I have a website that is forced to non-www (by the hosting). I have access to htaccess file and I am trying to redirect some of my pages to another domain. The www works, the non-www (which is the current one) it's not redirecting. Here it is what I have in htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Redirect 301 /my-slug-here https://anotherdomain.example/some-slug-here/
</IfModule>

Solution

  • Using Redirect from mod_alias and RewriteRule from mod_rewrite together is not recommended. You will get better results by implementing your rule with RewriteRule and putting it above your front controller rule.

    I wouldn't recommend putting non-WordPress rules inside the BEGIN WordPress section because WordPress could overwrite that whole section during an upgrade. I'd put your redirect rule above the WordPress section, even if that means using a second RewriteEngine On

    RewriteEngine On
    RewriteRule ^/?my-slug-here https://anotherdomain.example/some-slug-here/ [R=301,L]
    
    # BEGIN 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>