www.example.com not redirecting to HTTPS - wordpress

When we enter example.com in browser address bar it will redirecting to https://example.com and also inner pages working (redirecting to https site),
Issue 1:
When we enter www.example.com in browser address bar it is not redirecting to https://www.example.com and Site loading without https, but inner pages are redirecting to https
Issue 2:
https://example.com/file.php shows current content, but http://www.example.com/file.php shows old content, We checked in Incognito mode and clearing the caches
Issue 3: (this may be the root cause of the issues)
When we are seeing the $_SERVER parameters by accessing the url http://www.example.com/server.php shows HTTPS value as on
We print_r the $_SERVER in server.php file
Environment:
Godaddy VPS Server
Apache 2.3.1 (mod_rewrite enabled)
PHP 5.4.45
Wordpress
Valid SSL Certificate Configured - Required Ports are opened (80, 443)
In Wordpress Settings,
we placed https address for both Site url and Home page url as https://example.com
we used the below code in .htaccess for redirection
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.example.com$
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [L,R=301]
And we also checked in below sites and they are showing positive response for SSL
https://www.sslshopper.com, whynopadlock.com
Everytime We checked by clearing cache and cookies
DNS Records:
A (Host)
Host # Points To xx.xx.xx.xx with TTL 600 Seconds
CName (Alias)
Host www Points To # with TTL 1 Hour
Please suggest how can i resolve this issue with www.example.com to https

I am using below code in .htaccess file to redirect http request to https.
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC]
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST} is an invaild condition and it never returns true because you can not use server variable in RewriteCond's pattern. You can use Regex based pattern like the following :
RewriteCond %{HTTP_HOST} ^www\.example.com$

Related

WordPress 301 Redirect For IP Canonicalization

I need 301 Redirect for IP Canonicalization. So When user type http://example.com/ or example.com/ or www.example.com/ in a browser it must redirect to http://www.example.com/ .
The only thing you need to redirect is the non-www. http:// will be added by the browser.
Use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
# Support HTTP 1.0 (no host header)
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
This will preserve any part of the URL after the host and redirect anything that is not www. including justexample.com. For example a good policy can be to point ww. to the site to catch mis-spellings, amongst others. Personally I prefer to use a wildcard in DNS so anything.example.com gets redirected.

Domain address- misunderstanding in WordPress

I don't understand why this Url:
www.hortadascanas.com
and this Url:
http://hortadascanas.com
are not pointing to the same page. If you look the content, you can see that icons don't show up at the 2nd adress...Actually It seems to me that the 2nd Url links to an older version of the page.
I thought the "www" was a shortcut of "/public_html".
If I try with this address http://hortadascanas.com/location it is redirected to http://www.hortadascanas.com/location but the home page is not redirected.
What is happening ?
EDIT
This is what I have in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I guess I have to remove some lines because the site crashes if I add the lines you gave me....I have redirections loops...
EDIT 2
Should I delete this red line and recreate one pointing to the alias www.hortadascanas.com ?
This can be done by adding the following lines at the beginning of the .htaccess file in your public_html folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.hortadascanas.com [NC]
RewriteRule ^(.*)$ http://hortadascanas.com/$1 [L,R=301]
if you want to redirect both HTTP and HTTPS non-www Urls to www, you can combine rules as follows:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
For more information please read following :
https://www.siteground.com/kb/how_to_redirect_www_urls_to_nonwww/
Edit
In your case its because of CNAME in your cloudflare,please check complete guide for this,
https://help.ghost.org/hc/en-us/articles/223210747-Root-Domain-Setup-Using-CloudFlare
www.hortadascanas.com and hortadascanas.com are simply different hostnames.
They don't have identical DNS resolution:
[ ~ ]
[ user ][ user#workstation.local ] % host hortadascanas.com
hortadascanas.com has address 69.195.124.135
hortadascanas.com mail is handled by 0 mail.hortadascanas.com.
[ ~ ]
[ user ][ user#workstation.local ] % host www.hortadascanas.com
www.hortadascanas.com has address 104.27.134.235
www.hortadascanas.com has address 104.27.135.235
www.hortadascanas.com has IPv6 address 2400:cb00:2048:1::681b:86eb
www.hortadascanas.com has IPv6 address 2400:cb00:2048:1::681b:87eb
You end up talking to two different computers depending on which hostname you use.
Running the IP addresses through whois we can see that www.hortadascanas.com is handled by Cloudflare, who provide a CDN and caching service.
Actually It seems to me that the 2nd Url links to an older version of the page.
Presumably Cloudflare is loading the data from 69.195.124.135 and caching it … so it is an older version of the page.
Even if the two hostnames' DNS resolved to the same computer, you could still get different content. Name based virtual hosting allows multiple websites to share a single IP address. The Hostname is included in the HTTP request header so the server can determine which site to return.

Installed Wordpress Network with www in the Site URL. How do I fix this?

I made a huge mistake. I set up a network without changing sitename to non-www, so now example.com (without www) is a non-existing page. How do I fix this? Changes in
settings
DNS
htaccess
?
I've tried htaccess redirect but wordpress sees the first request and still says www is missing.
We can't edit the Wordpress source code to redirect, as it will be broken on future updates. We can't forward all requests to the www-version of that request as that will break all subdomains.
I solved this with some edits to the root .htaccess file.
# This is probably how your file starts already
RewriteEngine On
RewriteBase /
# Then you add a condition: if the host starts with example.com
RewriteCond %{HTTP_HOST} ^example.com.*$
# And add a rule: redirect that url to the same url just with prepended with www
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Here the file continues with other stuff from WP
RewriteRule ^index\.php$ - [L]

Issue with HTTP link not forwarding to HTTPS

I have recently implemented the HTTPS/SSL on a website. I have impleted it correctly and everything works correctly accept 1 issue.
When I got to www.domain.com or doamin.com directly it doesnt go to the https://www.domain.com instead it just stays on the non https link. How can this be fixed?
My site is on Wordpress and using W3TC Page Cache plugin
In the .htaccess file in the root of your server, or in the Apache config add a rewrite rule to redirect HTTP to HTTPS:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Test the rules on http://htaccess.madewithlove.be/.

Enable https using Rewrite Rule for specific URLs

I am new to Apache and I am finding it extremely difficult to understand the rewrite rule syntax.
Here is my setup:
I have a Wordpress MU site at mydomain.com
I have parked domains site1, site2.com that point to mydomain.com
What I want:
I want this URL http://mydomain.com/wp-login.php to redirect to the same address but with https first. I have seen several tutorials on how to do this but there is a problem.
My problem:
If I use something like
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
this also redirects my other sites to https versions because they are parked domains and effectively loading the same htaccess code; since the certificate I have is only for mydomain.com I get certificate errors.
I need to only secure pages on the mydomain.com but only the wp-login.php page. Another issue is that this page can move around in the URL, IE:
mydomain.com/wp-login.php
mydomain.com/somecracp/wp-login.php
mydomain.com/wp-login.php?xys=5653
I need a regex which will detect the domain name of mydomain.com and the wp-login.php anywhere in the URL. I then need to force https for them. Can anyone build me the code please?
UPDATE:
I may have found part of the issue. It would seem that the login page itself is also trying to do some redirection. When I access the admin section of the site http://mysite.com/wp-admin it detects that you aren't logged in; it then redirects you to the login page but appends a redirect_to query string at the end like so:
http://mydomain.com/subdomain/wp-login.php?redirect_to=http%3A%2F%2Fmydomain.com%2Fsubdomain%2Fwp-admin%2F&reauth=1
It seems to do this so that once you log in successfully it can redirect you to the admin section you previously tried to access. This "redirect_to" and what comes after it is the URL it will redirect to once you log in.
I won't to go into details as to why but is there a way I can force it overwrite the above URL to go to mydomain.com/wp-login.php with none of the rediret_to crap?
Try this:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule wp-login.php https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
The regex part of the RewriteRule checks to see if there is any instance of wp-login.php in the request, and if there is, redirect to https.
Try
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} wp-login.php [NC]
RewriteRule https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The rewriteCondition will check if https is off and request uri contains wp-login.php, then redirect.

Resources