How to properly force HTTPS on specific pages using Apache - apache

I would like to force my signup page to https, and allow all other pages to be browsed using https or http (e.g., http://www.example.com/signup should redirect to https://www.example.com/signup). I was able to force all pages to https, but cannot get only one page to redirect to https. The page just loads normally as http.
Here's the code I've been trying to use in my htaccess file:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^/signup(/.*)$ https://www.example.com/$1 [R=301,L]
For what it's worth (in case there is a conflict I'm unaware of), I am also using the following code to force all pages to redirect to www and to drop the .php from the file names in the URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php?/$1
Any thoughts on what I'm doing wrong?

Have your https rule like this:
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(signup/?)$ https://www.example.com/$1 [R=301,L,NC]
Remember that there is no starting slash / in RewriteRule.

Related

htaccess rewrite from http to https

Currently I redirect all http users (www or non-www) of upscfever.com to http://upscfever.com/upsc-fever/index.html
using
RewriteEngine on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "http\:\/\/upscfever\.com\/upsc\-fever\/index\.html" [R=301,L]
Now I want all users to shift to https so I modified as follows:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "https\:\/\/upscfever\.com\/upsc\-fever\/index\.html" [R=301,L]
So that all who type upscfever.com OR www.upscfever.com should go to
https://upscfever.com/upsc-fever/index.html - instead
Plus all links should be https. But its not working I get Page not found.
Your server has to setup the https first, depend on hosting vendor, if your hosting is vps you need to setup https for apache, install cert also.
You can find some instruction like this:
https://manual.seafile.com/deploy/https_with_apache.html
https://www.digicert.com/csr-ssl-installation/apache-openssl.htm
I think you want to make 3 different changes:
Change your .htaccess file to redirect requests to root to your custom index irrespective of the HTTPS or HTTP for the original request
RewriteEngine on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "https://%{SERVER_NAME}/upsc-fever/index.html" [R,L]
There is no R=301 part here because I'm not sure it is really wise to make permanent such a redirect to an obscure inner URL.
Redirect all other non-HTTPS requests to HTTPS (preserving the rest of the URL):
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Making this redirect permanent seems pretty safe.
Change all internal links in all of your HTML pages (or whatever backend generates them) to use protocol-relative // prefix or explicitly https:// instead of current http://. Preserve the protocol for the external links as is.
As for troubleshooting, you may use the Network tab of the Chrome DevTools (F12) to see exact server reply (note: enabling "Preserve log" and "Disable cache" flags is useful in such context)
You can do that using a single rule as follows in your site root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?upscfever\.com$ [NC]
RewriteRule ^/?$ /upsc-fever/index.html [R=301,L]
This will redirect both http and https URLs.
You may try something like 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]
I hope below code will do the work for you
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://famebooking.net/$1 [R,L]
just simply add above code in .htaccess below authorization header condition is written under RewriteEngine On
Let me know if that helps.

Using multiple rewrite rules .htaccess

I made the switch of my website from http to https. Now, I want to 301 redirect all http content to https.
I want to use this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The issue is I also have another rule already in place which redirects all non-www pages to www ones.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
How can I combine it so whatever link users write (http non-www ; http www ; https non-www) all redirect to https://www.example.com
Cheers!
You can use this single rule to add www and http->https in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
This rule should be kept just below RewriteEngine line and make sure to clear your browser cache when testing this change.

Redirect traffic from www. to store.

I am trying to make changes on .htaccess to redirect traffic that is in the form www.mysite.com/products/product_id or www.mysite.com/items/item_id to store.mysite.com/products/product_id or store.mysite.com/items/item_id
To redirect
mysite.com/products/*
mysite.com/items/*
to
store.mysite.com/products/*
store.mysite.com/items/*
You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteCond %{REQUEST_URI} ^/(products|items) [NC]
RewriteRule ^(.+)$ http://store.mysite.com/$1 [NC,L,QSA,R]

redirecting from www to non-www urls with .htaccess

I am redirecting an application with following code on my .htaccess file, the page is supposed to do the followings:
replace .php extension with .html
redirect from http to https
redirect from www to non-www urls
The extension .html is working fine and it is redirecting from http to https but the issue is to redirect from www to non-www, it is working properly on main url but when there is reference to a file then it is not working.
Say when i write www.ntestechnologies.com i get my desire url that is https://ntestechnologies.com but when i write www.ntestechnologies.com/index.html i get this https://www.ntestechnologies.com/index.html i don't need the www in this url as well please guide me, here is the code on htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.ntestechnologies\.com$
RewriteRule ^/?$ "https\:\/\/ntestechnologies\.com\/$1" [R=301,L]
RewriteRule ^(.*)\.html$ $1.php [nc]
You need only one RewriteEngine On.
You cannot use HTTP_HOST or REQUEST_URI in a RewriteRule. If you need to capture these values, you must do so in a RewriteCond
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(.+)
RewriteRule .* https://%1/$0 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1/$0 [R,L]
This removes the leading www, if present. At the same time, it redirects to HTTPS.
RewriteEngine On
# Redirects from HTTP to HTTPS. We use %{SERVER_PORT} as it's more reliable than %{HTTPS}
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Redirects www.example.com/... to example.com/...
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1%{REQUEST_URI} [R,L]
RewriteRule ^(.*)\.html$ $1.php [nc]

How to force rewrite to HTTPS except for a few pages in Apache?

I need to force redirect all the pages in Apache to HTTPS except for a few pages. How to write rewrite rule in Apache for this condition?
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/page1\/
RewriteCond %{REQUEST_URI} !^\/page2\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/page1\/ [OR]
RewriteCond %{REQUEST_URI} \/page2\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
The first rule-set will redirect all pages not accessed via HTTPS, and that are not /page1/ or /page2/ to the same URL but https://. The second rule-set will make sure that /page1/ and /page2/ are redirected back to http:// if they are accessed via https://.
A more simple solution:
RedirectMatch ^((?!\/(page1|page2)).*)$ https://%{HTTP_HOST}$1
This will redirect everything except page1 and page2 to https of the current host.