Rewrite all URL requests to https://www.example.com/$1 - regex

I have two domains, example.com and example.co.uk, that use the same application on my server.
I would like to rewrite the address of the URL depending on what the user types in. The only URLs I want are https://www.example.com and https://www.example.co.uk
In my .htaccess file I have the following:
# Turn on URL rewriting
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
If I goto http://www.example.com it doesn't add the https, if I goto http://example.com it does. What is the best way of making the ReWriteCond match the www url?

Have your 2 rules like this:
RewriteEngine On
# add www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# turn on HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
These 2 rules will take care of both of your domains.

Related

htaccess remove segment from url and redirect to https and www

I am deploying a Craft CMS site to a shared hosting account using Deployer.
The latest deployment is accessible from domain.com/current/public
My .htaccess file looks like the following, which strips current/public from the url and forces https:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
I now need to also redirect all urls to use www
How can I adjust my .htaccess to force www on all urls?
*** UPDATE ***
I have managed to solve the above with the following:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
When I go to example.com/admin it redirects to example.com/current/public/admin. How can I adjust my htaccess file to remove 'current/public' from admin urls?
Your http->https and www adding rule should be top most rule so that it works on original URI instead of a rewritten URI due to other rule.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.*)$ current/public/$1 [L]
Make sure to clear browser cache before testing this rule.
Also add this redirect rule in /current/public/.htaccess:
RewriteCond %{THE_REQUEST} /current/public/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

redirect non www to www for any tld

I am facing an issue for my .htaccess redirect to www from non www
I like to redirect any tld domain like .com or .co.uk etc.
I am using the following solution, from this link [Redirect non-www to www in .htaccess
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but it only redirect tld with .com/.net etc but can't figure out how to change this so that it can grab any tld like .co.uk or .co.in etc.
EDIT: I have sub-domain there as well so using following rule not gonna work.
RewriteCond %{HTTP_HOST} !^www\.
Thanks in advance.
Updated answer for subdomain:
# rule for forcing www on main domain
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# rule for removing www on sub domains
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
You are pretty close. You can use:
RewriteCond %{HTTP_HOST} ^[^.]+\.(?:com|co\.uk)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
If you have Apache 2.4+ then you can use %{REQUEST_SCHEME} variable:
RewriteCond %{HTTP_HOST} ^[^.]+\.(?:com|co\.uk)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

htacces ad redirect - is this right?

I need to redirect all my users to another site keeping the current page structure in order to redirect them to the right page on the new domain.
My goal is redirect only the main domain, not the subdomain so I create this snippet but I'm not sure this is right:
RewriteCond %{HTTP_HOST} domain.com
RewriteRule /(.*) http://otherdomain.com [R=301,L]
I already have this in my htaccess that redirect users from www to non-www site:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
How can I make sure that if a user comes from www.domain.com he will be redirect to the new domain?
You can place new rule just below earlier rule like this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]
However if there is only one domain hosted then you can replace those 2 rules by this single rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]

htaccess redirect to HTTPS except a few urls

I am new to the htaccess redirect stuff but want to do smth special - and I dont know whats the recommend way and dont know if this is still possible or not.
I have this in my .htaccess file:
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]
Now every URL is redirected to the HTTPS version - this is fine and necessery. But now there are a few exceptions.
For example these urls HAS to be HTTP instead of HTTPS:
http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
Is it possible to solve this with the htaccess and when its possible maybe you can send me a reference link or describe how to do this.
Edit
I now have this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(/commerce_paypal/*)\s [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The goal is that every (!) url gets redirected to HTTPS except ANY url which has commerce_paypal at the beginning.
For example:
mydomain.com/commerce_paypal <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https
You can have a RewriteCond to add exceptions in the http->http rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except some selected URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /commerce_paypal/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
worked for me. I tried many similar condition rewrites without luck.

apache .htaccess ssl to only one site

I have an .htaccess file for some domains sitting on same folder but leads to different content that is determined by php according to the domain name.
I want only one of them to redirect all requests to ssl domain.
How that's can be done?
When I use redirect rules it transfer all domains:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%mydomain.com%{REQUEST_URI} [L,R=301]
If you want to skip mydomain.com from this http->https redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%mydomain.com%{REQUEST_URI} [L,R=301]