A better way to URL Rewrite this .htaccess - linux

Could there be a better way of doing this I wonder. It is doing the following:
A) routing mydomain.uk to www.mydomain.uk
B) routing anything non http to https://www.mydomain.uk
I dont use linux based servers much so im sure there is a better way of doing this. End result should be any base url should end up at https://www.mydomain.uk
Heres the code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.uk\/" [R=301,L]

These 2 rules can be easily combined into one as this one:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.mydomain.uk%{REQUEST_URI} [L,R=301,NE]

You can use OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule (.*) https://www.mydomain.uk%{REQUEST_URI} [R,L]
The rule RewriteCond %{HTTP_HOST} ^mydomain.uk$ can also be more restrictive and catch any host which is not www.mydomain.uk.

Related

redirect incomplete url to https://

I need to edit my .htaccess so the following happens:
theeatalianjob.com -> https://www.theeatalianjob.com
www.theeatalianjob.com -> https://www.theeatalianjob.com
http://www.theeatalianjob.com -> https://www.theeatalianjob.com
the following:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
only adds the https:// in front of the HTTP_HOST how do I write a if statement that adds www. if missing?
Thank you for your help.
Your answer will do 2 things:
Add www if missing
Redirect http to https
So if URL is http://domain.com then there will be two redirects and that is bad for SEO and for user experience also.
You can use a single rule to do both redirects like this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I think I have found the solution to my own question but feel free to suggest more elegant ones...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force SSL, WWW and remove index.php htaccess

How can I achieve all of this using htaccess. Thus far I have -
To enforce SSL
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]
And To remove index.php
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]
it seems to work for the most part but when i enter something like - "example.com/index.php", i get an error. It is redirected to
https://www.example.com/https://www.example.com/
I know why its happening but can't figure out a rule to combine everything I need and make it work.
Going to take a guess, you're missing the L flag in the first rule:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Without the L flag, the request gets marked as "redirect" but continues on to have the other rules processed. And by the result you're seeing, it looks like the 2 rules are interferring with each other.
But is there any way to reduce the redirects when I type example.com/index.php
Not sure if this will work, because I can't test it, but maybe something like this?
RewriteCond %{THE_REQUEST} ^.*/index.php [OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*?)(index\.php)?$ https://www.example.com/$1 [R=301,L]

htaccess force ssl for a particular domain

How do I force SSL for a particular domain currently I have
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is working, however I have several add on domains on my hosting and when I try to access the other add-on domains, the add-on domains are also forced to use SSL.
I tried this:
RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.exampledomain\.org$
RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L]
But it is giving me an infinite loop.
This should work:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} exampledomain\.org$ [NC]
RewriteRule ^ https://www.examplemydomain.org%{REQUEST_URI} [R=301,L,NE]
It looks like you're doing 2 different things here. You're adding a www when one is missing, and you're forcing SSL when it isn't used. So there's 2 different conditions and either one being true should force a redirect. That means you want to use the [OR] flag, but the way you were using it breaks if the request is already SSL. Try:
RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.examplemydomain.org/$1 [R=301,L]
try:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.){0,1}exampledomain\.org$
RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L]

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 RewriteRule non-www to www without explicity specifying domain

I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]
The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks
I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
And to cover Cags’ concerns a litte bit, you can also try this rule:
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources