Forcing HTTPS & Non-WWW on all folders with exceptions in place - .htaccess

I'm running into an issue where by my subdomains are incorrectly getting www appended to them via a htaccess rewrite rule...
My folder structure is as follows:
/public_html/index.html (A maintenance page just in case)
/public_html/.htaccess
/public_html/websitename
/public_html/subdomain
/public_html/testsite
/public_html/clone
My /public_html/ that's located in my .htaccess looks as follows
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} websitename.com$ [NC]
RewriteRule ^(.*)$ /websitename/$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.website.com$ [NC]
RewriteRule ^(.*)$ /subdomain/$1 [L]
</IfModule>
Which works perfectly for websitename.com, which forces the URL to be rewrote to https://www.websitename.com
However it makes my subdomains get incorrectly rewrote to https://www.subdomain.websitename.com when it should be https://subdomain.websitename.com
I don't want to have to put the forcing of HTTPS & WWW in the individual website folders... rather, I'm looking for a solution to make subdomain exempt from the rewrite rule.
I tried adding the following condition but it didn't help:
RewriteCond %{HTTP_HOST} subdomain.websitename.com$ [NC]
Anyone have any idea what I can do to get around this issue?

Remove:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
And add:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For multiple domains you can use:
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L]

Related

RewriteRule for subdomain to subfolder .htaccess

I'm trying to accomplish the following:
non-http force to https - works
www force to non-www - works
website loaded from subfolder (/web) - works
test.example.com load different subfolder (/test) - does not work
4, Does not work, the condition is met to go to /web. Can't understand how to change this into /test
the .htaccess code I use:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]
With your shown samples please try following htaccess rules file here. Please make sure to clear your browser cache before testing your URLs. In case you have further more rules(apart from shown ones) then make sure these Rules are before those rules.
RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]

.htaccess rewrites results in www.www

What I want to achieve should be quite simple:
Redirect all traffic to HTTPS and the www. subdomain. And to achieve this I used the following rule:
# Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*) https://www.%1/$1 [R=301,L]
</IfModule>
However, if I follow a link like this:
http://www.example.com
I end up here:
https://www.www.example.com
So then I found this question: .htaccess: http://www redirects to www.www
And I completely replaced the rule I used above with the rule suggested in the accepted answer:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [R=301,L]
However now I get the ERR_TOO_MANY_REDIRECTS error, and the site completely refuses to load.
Can someone help me out here?
Check this rewrite in your .htaccess file, maybe it help
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Redirecting from IP address to domain name in HTTPS via .htaccess

I am trying to Canonicate an IP to redirect to the domain name, but for some reasons it won't work and constantly having https://www.111.111.111.11.
Here is what I am using in my .htaccess
RewriteEngine On
RewriteBase /
#Uncomment lines below when uploading to the live server
RewriteCond %{HTTP_HOST} ^111\.111\.111\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.org$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^(www\.)example\.org$ [NC]
RewriteRule ^.*$ https://www.example.org[L,R=301]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example\.org/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Additionally, plesk takes care of a safe SEO redirect from example.org to www.example.org and also from http to https.
I am not very familiar with .htaccess rules, but even to me it looks like I have some unnecessary lines in there.
Can anyone please help?
Thanks in advance
In public directory, you must be sure that 000-default.conf file has AllowOverride All set, so the settings in your .htaccess file will be used.
Use these following in your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^111\.22\.33\.44$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]

Forcing WWW And HTTPS (SSL) In .htaccess -- Requiring Assistance

I have found a thousand other topics asking for help with this, but none of their solutions seem to work for some reason.
I just purchased SSL for my domain a couple of days ago because I am accepting credit/check cards on my site and I want my customers to feel and be secure.
Anyways, this is what my .htaccess file looks like at the moment:
php_flag display_startup_errors off
php_flag display_errors off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^buy-wow-accounts index.php [NC]
RewriteRule ^sell-wow-accounts sell.php [NC]
RewriteRule ^about-khaccounts about.php [NC]
RewriteRule ^buy-sell-wow-accounts-faq faq.php [NC]
RewriteRule ^khaccounts-feedback feedback.php [NC]
RewriteRule ^payment-plan payment-plan.php [NC]
RewriteRule ^customer-login customer-login.php [NC]
RewriteRule ^customer-center customer-center.php [NC]
RewriteRule ^privacy-policy privacy.php [NC]
RewriteRule ^buy-world-of-warcraft-wow-accounts/page-([0-9]+) listing.php?pageid=$1 [L,NC]
RewriteRule ^buy-world-of-warcraft-wow-accounts listing.php [L,NC]
RewriteRule ^world-of-warcraft-wow-acc/([^/]*)\.html$ account.php?acc=$1 [NC]
What I want to do is force WWW if it is not already in the URL and ensure that HTTPS (SSL) is forced as well. I have a ton of links out there to my site and most of them are just links like 'www.khaccounts.net', 'http://khaccounts.net', and 'khaccounts.net'.
I want to ensure that each of these different old links will be forced into having WWW and HTTPS. In order words, I want people to have the URL - 'https://www.khaccounts.net' no matter what URL they took to get to my site.
Thanks!
Right after your www rules add:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Also, change your www rule to https:// so that there aren't 2 redirects.
Looks like you're rewriting all of your urls to the root. Why don't you try:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
This works in showing the proper URL when khaccounts.net is hit, but still throws a redirect loop.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.khaccounts.net/$1 [R=301,L,QSA]
Try replacing these lines :
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
With this :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.khaccounts\.net$ [NC]
RewriteRule ^(.*)$ https://www.khaccounts.net/$1 [R=301,L,QSA]

.htaccess Remove WWW from URL + Directories

This seems to be a non-issue for many people (read: I can't find an answer), but I would like to update the following htaccess code to not only remove the 'www' from the URL, but also any sub-directories that are accessed.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
With this, http://www.example.com/any/ resolves fine, but I want it to redirect to http://example.com/any/ as with the root.
I had the same problem (trouble stripping 'www' from URLs that point to a sub-directory on an add-on domain), but after some trial and error, this seems to work for me:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Redirection code for both non-www => www and opposite www => non-www. No hardcoding domains and schemes in .htaccess file. So origin domain and http/https version will be preserved.
APACHE 2.4 AND NEWER
NON-WWW => WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
Note: not working on Apache 2.2 where %{REQUEST_SCHEME} is not available. For compatibility with Apache 2.2 use code below or replace %{REQUEST_SCHEME} with fixed http/https.
APACHE 2.2 AND NEWER
NON-WWW => WWW:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
... or shorter version ...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
... shorter version not possible because %N is available only from last RewriteCond ...
I think you're close, but try the following:
# force non-www domain
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
Not sure exactly what you mean about sub-directories, but this follows your example.
I use this code. If my visitor does not have www in his url then this condition adds www with url, otherwise no need to add www with url ( because he already has. :) )
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.YOUR-SITE\.com$ [NC]
RewriteRule ^(.*) http://www.YOUR-SITE.com/$1 [L,R]
Hello, the code works perfectly, except that it passes with the www in a url with some value and slash at the end it shows the parameter and value in the url.
RewriteEngine On
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(!.(\.gif|\.jpg|\.png|\.swf|\.css|\.js|\.txt|\.php|\.htm|\.html)|.+[^/])$ /$1/ [R=301,NC]
RewriteRule ^(.[^.*]+)\/$ ?jogar=$1 [NC]
Options -Indexes
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http:\/\/%1%{REQUEST_URI} [R=301,QSA,NC,L]

Resources