I have a Website. Where I need to redirect all the pages from HTTP to HTTPS. But there are two page which should not served over HTTPS.
Home Page - www.hellomysite.com
Dealers Page -www.hellomysite.com/dealers
Even if user has entered the url as https://www.hellomysite.com/dealers, it should be served over HTTP. http://www.hellomysite.com/dealers
I googled & found number of links but none of them is redirected.
.htaccess
#Redirect all request to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.hellomysite\.com*
RewriteRule ^(.*)$ https://hellomysite.com/$1 [L,R=301]
#RewriteCond %{HTTPS} on [OR]
#RewriteRule ^https://hellomysite.com/dealers$ http://hellomysite/dealers [R=301,L,QSA]
If I try anything more, then I get an error on opening the site as
This website has too many redirects
How do I redirect Home Page & the dealers page to HTTP.
If i understand you the following code will solve it :
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{SCRIPT_FILENAME} !\/index\.php [NC]
#the above line will exclude https://www.hellomysite.com/index.php
# from the following rules
RewriteCond %{SCRIPT_FILENAME} !\/dealers\.php [NC]
# the above line will exclude the https://www.hellomysite.com/dealers.php
# from the following rules
RewriteRule (.+) https://%{HTTP_HOST}/$1 [L,R=301]
# above line will force every pages and directories ,except those who
# excluded above and the main root , to redirect from http to https
# (.+) means not to consider http://www.hellomysite.com/ and if you
# change it by (.*) it will be considered
Now you can force entire web site to redirect from http to https except www.hellomysite.com and www.hellomysite.com/dealers.
Note : please make sure that you empty browser cache before testing above code
Try :
#Redirect all request to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule !^$|dealer https://hellomysite.com%{REQUEST_URI} [L,R=301]
Related
I'm trying to get a website I'm administrating to redirect only one page over HTTP, while the rest of the site will be sent over HTTPS.
The server is an Ubuntu Server 16.04.3 setup, with WordPress 4.9 installed. The certificate is from Let's Encrypt.
According to another post I was reading, one of the solutions given was to add something similar to the following code in the site's .htaccess file:
# redirects non-www page requests to www version
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L,NE]
# adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# 3 pages to be on https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(order-form|employment-
application|about/contact-us)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]
# All other pages have to be on http
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(order-form|employment-
application|about/contact-us)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301,NE]
However, after applying this solution and restarting the apache server, the entire site went into a state of constant redirects and timeouts. As mentioned, I just need one page and one page only to be on HTTP, and the rest of the site can safely be on HTTPS. Any solutions would be greatly appreciated.
Personally, I would try to come up with an alternative way to pull in that resource from wherever it lives on HTTP. Is it a script? An image? There has to be a better way to pull it.
I have read a lot about this, but i still can't figure it out for my case. I am trying to force all http requests to go to https except for 2 pages. Those 2 pages requests have parameters passed to them. Here is the code i am using currently:
# Force go to https if user isn't asking for a url that has 'register_user.php' or 'register-customer' inside the url:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/register_user.php$ [OR]
RewriteCond %{REQUEST_URI} !^/register-customer$ [NC]
RewriteRule ^(.*)$ https://www.myWebsite.com/main-folder/$1 [R,L]
# Force go to http if user is asking for a url that has 'register_user.php' or 'register-customer' inside the url:
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/register_user.php$ [OR]
RewriteCond %{REQUEST_URI} !^/register-customer$ [NC]
RewriteRule ^(.*)$ http://www.myWebsite.com/main-folder/$1 [R,L]
This code doesn't do the required job. Is there something wrong with it?
You should use THE_REQUEST instead of REQUEST_URI as REQUEST_URI may change due to other rules.
Also you should keep these 2 rules at the top before other rules:
# Force go to https if user isn't asking for a url that has 'register_user.php' or 'register-customer' inside the url:
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/(register-customer|register_user\.php)[?/\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}/main-folder%{REQUEST_URI} [R=301,NE,L]
# Force go to http if user is asking for a url that has 'register_user.php' or 'register-customer' inside the url:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /(register-customer|register_user\.php)[?/\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}/main-folder%{REQUEST_URI} [R=301,NE,L]
Make sure to clear your browser cache before testing this change.
I need to find the best rewrite rule for my website...
I have 2 domains example.it and example.com but I have SSL under example.com only.
What I need: each user request should be redirected to https://www.example.com/...
Now I'm using this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but if I digit http://example.com its redirect me to https://example.com, not www.
Also if I do a GTMetrix test I got an F value under Avoid landing page redirects:
Avoid landing page redirects for the following chain of redirected URLs.
http://example.it/
http://www.example.com/
https://www.example.com/
how can I solve? Thanks.
You can do all this in a single redirect rule and avoid multiple redirects. Have your .htaccess containing this rule only:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache when testing this.
I have a website that I'm deploying to a server with https enabled. So my client's requirements are as follow:
Force https.
Keep www in the url, in order to get urls as https://www.example.com, not https://exambple.com so the 3ws are forced too.
I used this rule on the .htaccess file in order to get the desired effects:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/es/$1/ [R=301,L]
But when testing page speed using PageSpeed Insights I get a poor Server Response Time of 1s. And debugging using Chrome Developer Tools I see that the response time of the website after the last redirection is about 300ms so, the redirections are increasing the response time.
So, my question is, how can I force https and www in front of the url while minimizing redirects?
Try doing it like this:
Force SSL:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Force WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You may use below rules which will append www and https with your URL.
RewriteEngine On
# Ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Ensure https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
i use mod_rewrite to allow "friendly URLS" to replace search parameters, which works fine.
However i need to now redirect all traffic to https:// but allow only some pages to remain on http://
(i need to do this so my referrals are collected by the sites i send traffic to)
Over the years i have added to the .htaccess file, most works OK but i dont fully understand it, so it may be getting messy too :-(
i have the below (i have cut out anything i dont think is relevant)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
The above works OK, but redirect ALL pages to https
I have tried adding /changing the https redirect part to the below to allow urlout.php not to be redirected, but this just loops and i get a browser warning that the page has too many redirects :-(
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have tried a few other things too, but they either dont redirect and everything goes to https, or i get a loop error or internal 500 error...
Basically i want all pages to go to https, but not urlout.php
Any advice on any of the code above would me much appreciated!
Answering my own questions:
i managed to do this, was a silly mistake of me not adding [NC] after the first condition...
However, for others, the below code will redirect ALL to https:// from http:// with the exception of 1 page (urlout.php in my case)
# redirects any http:// traffic to https:// with exception of urlout.php
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/urlout\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I needed to do this, so that sites i send traffic to can see my domain as the "HTTP referrer".
I however found a simpler way to pass the referrer from https to http (although browser specific!) of adding to the HTML head:
<meta name="referrer" content="origin">
My ref for this was (among others): moz.com's Meta Referrer Tag Blog Post
If you want to redirect some spacific pages to https, forexample, To redirect :
http://www.example.com/page.html
to
https://www.example.com/page.html
And
http://www.example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
RewriteCond %{HTTPS} off is important to avoid redirect loop as it skips the rule for https requests.