WWW to non-WWW Redirect with PHP
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
header('Location: '. $pageURL);
Would redirect the user to the exact same page, www. intact.
So, to get rid of the www. , we just replace one line:
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= substr($_SERVER['SERVER_NAME'], 4).":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= substr($_SERVER['SERVER_NAME'], 4).$_SERVER["REQUEST_URI"];
}
return $pageURL;
And that should work.
By the way, this is the method that is recommended by Google, as it keeps https://
intact, along with ports and such if you do use them.
As Gumbo pointed out, he uses $_SERVER['HTTP_HOST']
as it comes from the headers instead of the server, thus $_SERVER['SERVER_*']
is not as reliable. You could replace some$_SERVER['SERVER_NAME']
with $_SERVER['HTTP_HOST']
, and it should work the same way.
Redirect non-www to www using php header() function
You can do it like:
if (strpos($_SERVER['HTTP_HOST'], 'www') === false) {
$protocol = isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)
? 'https'
: 'http';
header(
"Location: $protocol://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
true,
301
);
}
Redirect from www to non-www in php?
You could create a .htaccess file with the following content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
The answer in this question explains a solution using PHP instead.
Redirect http to https and www to non-www in .htaccess
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected
to the https/non-www address
OR
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]
Redirect from www to non-www not working
You need to move this block before the other expressions:
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Although you have the L
tag in the previous one, so it should be the last rule applied, it doesn't work (probably because your activate the RewriteEngine again later so it resets the rules, but if the L
flag was active in the first rule, it wouldn't apply the www
one anyway).
The rule of thumb is: first do your cosmetic changes to the domain, then apply the functional rules.
You also don't need to open 2 IfModule
conditions. Combine the 2 in one as this:
<IfModule mod_rewrite.c>
# (1)
RewriteEngine On
# (2)
Options +FollowSymlinks
# (3)
#Options +SymLinksIfOwnerMatch
# (4)
#RewriteBase /
# (5)
#RewriteOptions <options>
# (6)
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]
# RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
How to redirect non-www to www in my domain
You can specify the domain you want to redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
redirection issue in www to non www with ssl/https
Try this out:
To redirect www to non-www (while using SSL)
RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://your_domain.com/$1 [R=301]
The last 2 are meant for online stores using SSL or to prevent any SSL errors while using SSL on some pages.
Update1:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
Update 2
RewriteEngine On
Make all http use https:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://xxx.yyy/$1 [L,R=301]
Make only www https use the non-www https:
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www[.].+$
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [L,R=301]
Update 3:
Check out this issue over here:
.htaccess redirect www to non-www with SSL/HTTPS
Related Topics
PHP Datetime Microseconds Always Returns 0
How to Get Cookie's Expire Time
Difference Between Eloquent\Model::Get() and All()
PHP - Count Specific Array Values
Display Thumbnailphoto from Active Directory in PHP
PHP Authentication with Multiple Domains and Subdomains
PHP Exec: Does Not Return Output
Browser Displays � Instead of '
Rsa Encryption/Decryption Compatible with JavaScript and PHP
How to Use MySQLi_Query() in PHP
How to Setup Docker + PHPstorm + Xdebug on Ubuntu 16.04
Check for Duplicates Before Inserting
How to Take a Photo and Send to Http Post Request with Android
How to Post Button Value to PHP