https non www redirect - php

I'm trying to get all my URLs to redirect to https:// without www.
If someone puts in www. or http:// or http:// they are redirected to https:// the problem comes if they put https://www. then it doesn't redirect to https://website
I'm using the following in my htaccess, any suggestions?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This single rule can take care of both requirements:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to remove both of you shown rules and test in a new browser to avoid old cache.

Related

how to redirect non www url become www. into htaccess

i just want to setting file .htaccess for redirect non www url to www. but the case is not common.
if the url like this https://<domain name>.com become https://www.<domain name>.com
i already set the code like this. but didn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
that code is working if we type just <domain name>.com in browser. but when someone type manualy on browser like this https://<domain name>.com i mean type manualy the https:// can it turn into https://www.<domain name>.com
so for this .htaccess i want to do result like this
if type
<domainName>.com it become https://www.<domainName>.com
www.<domainName>.com it become https://www.<domainName>.com
https://<domainName>.com it become https://www.<domainName>.com
http://<domainName>.com it become https://www.<domainName>.com
please help.
You may use this rule in site root .htaccess:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Try this:
# uniform host name
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com$0 [R=301,L]
It includes a deep link forwarding.
You can try this
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect all www or non www page to https://www except one page

I am trying to enforce https://www. on all of my pages except hotels.php.
My current code below is adding www. twice, i.e. https://www.www.example.com
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/hotels.php\/
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
Let me know what I am missing here which causing issue.
Thanks
Try these rewrite conditions:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except your desired URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/hotels.php/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for your desired URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /hotels.php/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You have to check if www is in the request URI as you check HTTPS. Try this:
//If has not www and it's not a subdomain adds www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
//If has not https and it's not hotels.php add https
RewriteCond %{REQUEST_SCHEME} !https
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Not a master with Htaccess regex, bit this works for me:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>
Hope it helps! There's lot of answers to this on StackOverflow... pretty sure that probably where I got the above!

.HTACCESS subdomain redirecting

I am having an issue with subdomain.. i have a subdomain content.domain.com
which i want to use for cookieless domain content..
but when i wrote it in website.. its automatically add full website url.. which i have added in .htaccess
can you please let me know how can i stop redirecting the subdomain ....
Please help......
here is code for htaccess for domain redicretion
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can combine both rules into a single rule
Using a RewriteCond you can omit a subdomain.
Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?!content\.domain\.com)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your browser cache while testing this change.

How to redirect domain with www or without www

I am using the following rewrite rule to redirect domain that has or has not www.
RewriteCond %{HTTP_HOST} !^www\.[NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It works fine for any sub domain name like www.domainname.com/abc.php or domainname.com/abc.php. But not adding https in case of www.domainname.com or domainname.com.
I tried adding one more rule
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$
RewriteRule ^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But, its not working.
Just use this. Replace example.com with your domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

How to make redirect from http to https

How to redirect from http to https:
1. redirect http://example.com to https://www.example.com
2. redirect http://www.example.com to https://www.example.com
3. redirect https://example.com to https://www.example.com
Like facebook, google?
Please help me with code in .htaccess or another.
just add below to your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
To handle all these 3 redirects in a single rule you can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Categories