301 Redirect for All Subdirectories - wordpress

I am changing domain names for a WordPress site and want to set up a structure like so:
If a user accesses olddomain.com/page, he will be re-directed to newdomain.com/page, etc. In other words, only the pre-slash part of the domain would change. I want to do this for all pages under the old domain.
So far, I have only been able to get olddomain.com to re-direct to newdomain.com, but not any of the sub-directories (for example, olddomain.com/page does not re-direct to newdomain.com/page). Here is my .htaccess file:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
# 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>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
Does anyone know what I did wrong?

You must put all of the redirect rules before your routing rules. The wordpress rules route everything to index.php, including anything you intend to redirect. So the redirects must happen before any sort of internal routing happens.
Simply put your redirect rule before the wordpress rules:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
# 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>
# END WordPress

Change your .htaccess to the below code on the old domain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>
This will redirect the entire website on a page by page basis.

Related

My https website is redirecting to my old http website

a while ago i made a copy of my wordpress website and restore it on a ssl/https verficated account and since that, my new site have been redirecting to http.
I've tried with the htaccess code and it doesn´t work. I'll paste my code below:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://voladurascontroladas.com/$1 [R,L]
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.5.20]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTPS_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# 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>
# END WordPress

redirect address to directory htaccess

Pretty straight forward, I have an address www.example.com, and I need to redirect it to a subfolder which the actual website resides in.
I figured out how to redirect a http protocol request to https but this one I couldn't from what I've read.
This is my htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/subfolder/ [R=301,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
AddHandler application/x-httpd-php55 .php .php5 .php4 .php3
Use a Negative lookahead in regex like:
RewriteRule ^(?!subfolder/)(.*) http://www.example.com/subfolder/$1 [R=301,NC,L]

Wordpress SSL issue for addon domains in htaccess

I have an wp installation on the main domain and a few addon domains which are unrelated.
I installed really simple ssl plugin to handle all un-secure requests made by my site, but all the addon domains are affected and they require also ssl certificate.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ https://www.example.com/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.2.10]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
Without the Really simple ssl it works ok, but this part of the htaccess file affects all subdomains. Also i tried to add
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
in the ssl conditions but now all addon domains are redirected to the main domain.
Try this .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /WORDPRESSROOT/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /WORDPRESSROOT/index.php [L]
</IfModule>
# END WordPress
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.2.10]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
Change WORDPRESSROOT to the folder where your wordpress is located otherwise delete it and change it to /index.php
I also use Really Simple SSL but it only affects the folder where the .htaccess is located in. Otherwise try to move your website to /wordpress/ and redirect your site to your root folder.
Here's the documentation:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
EDIT: SAVE YOUR OLD .HTACCESS FIRST.

htaccess rewrite rule for sub domain 301 redirect - WordPress set up

I have been looking into this and had a question for a 301 redirect. I am using WordPress and used to have about 5 different sites using subdomains that are now just going to be one site at the root level.
So basically I want sub.yourdomain.com, sub2.yourdomain.com and sub3.yourdomain.com to redirect permanently to yourdomain.com. Wordpress already has a rewrite rule so was not sure if I add my 301's to that or create a new rule.
Here is what I have:
# 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>
# END WordPress
# BEGIN SEO Redirects
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.yourdomain.com$
RewriteCond %{HTTP_HOST} ^sub2.yourdomain.com$
RewriteCond %{HTTP_HOST} ^sub3.yourdomain.com$
RewriteRule ^(.*)$ http://yourdomain.com/ [R=301,L]
# END SEO Redirects
You can keep this rule before default WP rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sub|sub2|sub3)\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect all pages to new domain

I want to redirect all the pages of my website to a new domain with a redirect. I've managed to get it working on domain level but not on the subpages.
So olddomain.com redirects to newdomain.com but olddomain.com/contact doesn't redirect to newdomain.com. It still shows the old domain.
I've used multiple rewrites rules but none of them seem to works. Any help is much appreciated.
This is the code that I'm using right now in my .htaccess file
# 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>
# END WordPress
RewriteCond %{HTTP_HOST} ^racentegenkanker\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.racentegenkanker\.com$
RewriteRule ^(.*)$ "http\:\/\/againstcancer\.nl\/$1" [R=301,L]
For a full website redirection, you can use this (entire) .htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://againstcancer.nl/$1 [R=301,L]
EDIT
For two domains running side by side, with one redirecting the other :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} racentegenkanker\.com$
RewriteRule ^(.*)$ http://againstcancer.nl/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources