htaccess: How to redirect to non-www domain in subfolder? - apache

I'm using this code in my htaccess file:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTPS_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
This redirects to the non-www version of a domain, but if I'm in a subfolder like https://www.example.com/test/ nothing happens. How can my code affect subfolders too?

HTTPS_HOST does not exist, read the Apache HTTPD documentation.
The following variables provide the values of the named HTTP request
headers.
Name
HTTP_ACCEPT
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_REFERER
HTTP_USER_AGENT
Given that HTTPS_HOST doesn't exist when RewriteCond %{HTTPS} on is matched you're rewrite wasn't working at all.
You should use in both cases HTTP_HOST, and given that your rewrites are identical for both HTTP and HTTPS why don't simply have:
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
In case the suggested solution does not work I suggest to enable rewrite log:
LogLevel alert rewrite:trace3
Pay attention to not deploy such logging in production because it affects server performances.

Related

Using APACHE REWRITE To Force One Specific Page To Be HTTP (.htaccess)

I have tried using some rewrite to change my /welcome.php page to http. it is located in the root directory, but what I have done has seemingly forced all pages on my site to http instead of just the one. Here is what I have tried:
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteRule (.*) http://w5unt.ga/$1 [R=301,L]
#Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As you can see I am also force removing WWW from all urls at the same time I am trying to force my one page (welcome.php) to be http. I believe the error is in this bit of code
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However I am not sure how to syntactically correct my issue, any advice?
Try with below I've edited the part of your rule and made a check to exclude welcome.php.
RewriteEngine On
#Force remove WWW
RewriteCond %{HTTP_HOST} ^www.w5unt.ga
RewriteCond %{REQUEST_URI} !^/welcome.php
RewriteRule (.*) https://w5unt.ga/$1 [R=301,L]

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.

Rewrite subdirectory to https

I have been trying to rewrite a subdirectory from HTTP to HTTPS to no avail. I have looked at other posts and tried to implement the solutions:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} subdirectory
RewriteRule ^(.*)$ https://www.exmaple.com/subdirectory/$1 [R,L]
and also
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.example.com/$1 [R=301,L]
and placing them either on the root directory .htaccess file or the Plesk's Apache and Ngnix settings. But whenever I typed
http://www.example.com/subdirectory/some.html
I will always get
https://www.example.com/subdirectory//subdirectory/some.html
which is of course 404 not found. Any assistance is appreciated.
To force HTTPs for just a single directory then what you can use is:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(subdirectory)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(subdirectory)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
What is the above doing? The first condition checks if HTTPs is not on, if not, then it will check for the directory /subdirectory/ if found it will force this directory and only this directory to HTTPs.
The second set of rules is basically the opposite, forcing everything to HTTP except for the directory /subdirectory/.
Make sure you clear your cache before testing this.

Redirect http to https via .htaccess not working

I've seen a few questions like this on here, but no answer seems to work. I just can't seem to figure this out. Here is my code:
# enable basic rewriting
RewriteEngine on
# enable symbolic links
Options +FollowSymLinks
# Primary Domain - Force the use of "https"
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Primary Domain - Force the use of "www"
RewriteCond %{http_host} ^example.org [nc]
RewriteRule ^(.*)$ https://www.example.org/$1 [r=301,nc]
# Primary Domain - .com to .org
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
# Primary Domain - .net to .org
RewriteCond %{HTTP_HOST} ^example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
So whenever I go to www.example.org or http://www.example.org it will NOT redirect. This is what is frustrating me. If I go to example.org or any of the other redirects, it works though. Am I setting this up wrong?
Also, where it says "HTTP_HOST", do I leave it as is? Like do I replace it with my http host?
Amie if you want to force www and https both you can check for the absence of www. or check if https is not On then redirect. So either way it will redirect. example.
# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
Replace mydomain.org with your domain.
Then you can have a htaccess file like this. I took the last two rules and combined them so you have a smaller htaccess file.
# enable basic rewriting
RewriteEngine on
# enable symbolic links
Options +FollowSymLinks
# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
# Primary Domain - .com to .org Or .net to .org
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.(net|com)$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
Also, where it says "HTTP_HOST", do I leave it as is? Like do I
replace it with my http host?
%{HTTP_HOST} is a variable that contains the host sent with Http headers from the browser. So if someone requests http://mydomain.org/somepage.php that variable %{HTTP_HOST} will contain mydomain.org. You don't change it. You can use it to match things or set conditions like in the rules. Hope that answers your question.

Why won't this htaccess exclude condition work?

I'm forcing https on all pages on our site, but because of complications with some old plugins we use (needing to connect over http), I need to set up an exception to the https forcing for one directory.
I can't quite work it out though. The rule I think should work is giving me a 403 error.
Can someone have a look?
I've got a bunch of stuff for redirecting non-www to www:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^subdomain1.site.com$
RewriteCond %{HTTP_HOST} !^subdomain2.site.com$
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301]
Then there's the https forcing:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]
But if I add another condition to exclude the directory from this rule:
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/subdir/sub-subdir/
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]
I get an error where I see the content from my 404 page, but the URL is https://www.site.com/403.shtml - but I don't have a 403.shtml in my web root. (This is a WHM/cPanel-driven server)