Redirecting whole website by htaccess - wordpress

I am using a wordpress and trying to redirect my whole website to new domain. I want website structure to remain same that is in this way:
"www.domain.com" redirected to "www.domain.ca"
"www.domain.com/post1" redirected to "www.domain.ca/post1"
As my site is of wordpress so htaccess already has some commands. I haven't changed those commands just added some more by that my site home page starts to redirects but not the whole site.According to my research "RewriteRule ^(.*)$ ..." lets the whole website to redirect but unfortunately it is not. All of my htaccess code is given below. I have replaced my domain names in code.
old domain:www.domain.com
new domain: www.domain.ca
# 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} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.ca/$1" [R=301,L]
Thank You.

Related

Domain change: redirect 301 with htaccess does not work

I transferred my website from olddomain.de to newdomain.de and wanted to redirect every page of the old website to the new website (e.g. olddomain.de/contact -> newdomain.de/contact). When I enter the main page of the old website in the browser, I am redirected correctly but all other pages are not redirected.
I tried a lot of different redirect options and currently this one is implemented (the first IfModule already existed):
# 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>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.de/$1 [R=301,L]
</IfModule>
# END WordPress
Does anyone know how to correct the .htaccess file, so that all pages of the website are being redirected?
EDIT:
I managed to get all sites redirected by putting some code at the top of the IfModule like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.de$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.de$
RewriteRule (.*)$ http://newdomain.de/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Since this is working fine now, I wanted to change the address in the google search console but it says that the 301 redirection is not working. Does anyone have any suggestions here?
since your old site is not serving a WordPress instance anymore try removing the first part of your .htaccess file (you don't need it and may be causing the issue.
So let it just like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.de/$1 [R=301,L]
</IfModule>

wordpress permalink and https and folder redirect

I "inherited" a website at the department I am working at that is hosted on a machine we don't have root access to. I managed to force https with the .htaccess file, but I was unable to add wordpress permalinks to it also. The combinations I tried all ended up pointing every link to the front page.
This is the .htaccess we have now (the wordpress website is in a subdirectory):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^familydogproject.elte.hu$
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^ https://%{HTTP_HOST}/wordpress/%{REQUEST_URI} [L,R=301]
And this is the example wordpress gave for the permalinks:
# 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
Does anybody have any idea how to combine them?

Wordpress messed up our htaccess redirect, how to modify to work with Wordpress' htaccess code?

We had several domains parked on top of our main website. This was a normal HTML website and we used the following redirect so that the domain would 301 redirect to the proper url to avoid getting dinged by Google.
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,L]
So the above htaccess code just rewrites the url to www.ourmainsiteurl.com, which is what we want.
Now here's the problem... we installed Wordpress and it has the following default htaccess code:
# 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
I tried to add my redirect code in but that broke Wordpress. Can anyone tell me what I need to use to make this work?
Simply put I want all the parked domains (ie: parkedurl1.com, parkedurl2.com, etc) to be redirected to www.ourmainsiteurl.com with the htaccess file.
Make sure the redirect is before any wordpress rules:
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$ [NC]
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,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>
# END WordPress
If it's still not working, check to see if wordpress has something turned on that is redirecting, or adding/removing the "www" from the hostname.

htaccess redirect problems, Wordpress and static html site not redirecting properly

I'm having problems with a customer's website. I've created a new Wordpress site for him and now I'm having problems with htaccess redirecting.
Before I installed Wordpress on the server, there was two static basic HTML-sites. One in the public_html root using the main domain and a completely different subsite in a subfolder with a htaccess rewrite rule directing the subdomain into that folder.
Now there is Wordpress messing up the redirects. Wordpress of course made modifications to the original htaccess-file. In addition to that, the customer also had to change servers within the hosting provider to get newer php version so I had to write the htaccess file from scratch.
Here is the current htaccess-file :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteRule ^(.*)$ subdomainfolder/$1 [L]
Wordpress in installed in a folder named www and the url is being rewritten, it works beautifully. But I'm having problems with the subdomain. In the code above I've replaced the subdomain url with subdomainurl.com, the site lies in public_html/subdomainfolder/.
Now, when I type www.subdomainurl.com, the redirect works, the site is shown. But www.subdomainurl.index.html typed doesn't. It has to be written www.subdomainurl.com/subdomainfolder/index.html, so I'm clearly not writing the redirect right, adding the subdomain folder as well. I've tried to mingle with that htaccess but can't get it right. Everytime I try to change something I get a 404 in the main domain's Wordpress site.
At the moment these work:
www.maindomain.com/subdomainfolder/index.html
www.subdomainurl.com/subdomainfolder/index.html
And this doesn't, which I'm trying to do:
www.subdomainurl.com/index.html
I'm not a redirect expert, any help would be much appreciated.
Edit/Addon :
Here is the old code from the old server that redirected the subdomain to the subfolder and rewrote the url, it doesn't work anymore so I ditched it.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomainfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteRule ^(/)?$ subdomainfolder/index.html [L]
I don't see an entry in the htaccess file for just the subdomain and for the main domain.
Currently I'm using this code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !subdomainfolder
RewriteRule ^(.*)$ subdomainfolder$1
This loads the whole secondary domain (subdomainurl.com) as www.subdomainurl.com/subdomainfolder and it works like that (www.subdomainurl.com/subdomainfolder/index.html, www.subdomainurl.com/subdomainfolder/work.html etc.), but it still isn't what the customer really wanted. I still need to eliminate the subdomainfolder-part from the url by htaccess, so far I haven't had success. I suppose I haven't got it right on the final row of the code.

Wildcard domain redirect with WordPress

I have two domain names setup with sub-domains as follows...
blog.domain.com
www.blog.domain.com
blog.domain.info
www.blog.domain.info
Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)
To keep Google happy, I want everything to redirect to this one domain...
blog.domain.com
Here is what's inside the .htaccess file contained in the WordPress directory...
# 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
So I went into my cPanel and added a 301 Domain Redirect as follows...
blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).
cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]
The problem is that the wildcard portion does not seem to work.
When I go to blog.domain.info, I get redirected to blog.domain.com as expected.
But when I go to blog.domain.info/my-post, I do not get redirected at all.
How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.
Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.
Thank-you!
Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.
This all seems to be working.
Any comments appreciated.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT
It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [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>
# END WordPress
i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.
for example,
http://methodshop.NET/games/play/bubblewrap/index.shtml
would redirect to
http://methodshop.COM/games/play/bubblewrap/index.shtml
here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.
RewriteEngine on
RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]

Resources