website redirections different from .htaccess configuration - wordpress

I have web page created in Wordpress v5.4, Theme Twenty Twelve and just installed SSL certificate.
I want to have such redirection structure (redirection codes in rectangles), to optimize for SEO:
but according to Google Chrome Site Inspector I have the follwing one:
So the schema is not fully optimized as for two cases there are dwo cascade redirections whereas should be one.
What is weird, current schema doesn't reflect .htaccess file. My .htaccess file looks as follows:
# 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
So, my understanding of the code is that if url is typed in without https://, script rewrites it into version with https:// an nothing else, so there must be another place where other redirections (e.g. with www -> without www) are being made.
I have checked index.php - no redirections.
I also checked additional lines in .htaccess file don't overwrite those "hidden" ones, so if I simply add them I got multiple redirections error in web browser.
Could you please advise me what else should I do to get desired structure of redirections?

First rule redirect all www to https://domainname.com/, second all http to https://domainname.com/, one hop
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
</IfModule>
# 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’m guessing the redirect from www to non-www is due to the address you’ve set in your settings (WordPress dashboard → Settings → General).
For the redirect you want to achieve, you have to add rewrite conditions for the www. in your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

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>

Redirect to the same domain extension using WordPress

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>
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.
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

Force https+www in WordPress and non-WordPress subdirectory

I need to force https and www with .htaccess, and I'm looking for a solution that is not domain-specific, but will apply to any domain and any folder (using server variables).
The setup:
domain.com has WordPress installed
domain.com/folder2 is a physical folder and contains a separate site
The .htaccess file in domain.com currently contains:
# force www+https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# 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
This seems to work for domain.com, but not for domain.com/folder2: while https is enforced there, the www redirect doesn't work.
I tried placing a second .htaccess file in domain.com/folder2; that doesn't do anything either.
Questions:
How can I make these rules apply to domain.com/folder2 as well?
I've kept the WordPress block separate because WordPress would probably duplicate it when e.g. permalinks are changed via admin, and it doesn't find this block as-is. Is this correct, or is there a better way to consolidate the two blocks?
Thanks!
In .htaccess in /folder2, add the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
Delete your /folder2/.htaccess if you want to place this in your root dir.

wordpress naked domain vs www

I have a wordpress website somewebsite.com
If I visit it naked like that, it works brilliantly, however if I visit it from www.somewebsite.com I get
Warning: Cannot modify header information - headers already sent by (output started at /home/somewebsite/public_html/index.php:2) in /home/somewebsite/public_html/wp-includes/pluggable.php on line 1121
In the wordpress website general settings it's set to somewebsite.com
and in CPANEL I have the following
an A record with somewebsite.com pointing to the ip address
a CNAME record with www.somewebsite.com pointing to somewebsite.com
Just for kicks I changed the setting in the wordpress general settings to www.somewebsite.com which fixed it for if I was to visit it with the www but broke it for the naked domain, so I switched it back.
my .htaccess looks like
# 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
Any help would be greatly appreciated
Simple is best :-) Redirect non-www to www or www to non-www using .htaccess, so user is going to be redirected before loading any script in php.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
In your example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Remember to change chmod for .htaccess - it can be overwritten by WordPress in some cases.

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