rewrite url to non www multiple sites - regex

i have some sites they are running with the same files but different templates, my goal is to force use all www or non www domains.
this is my code:
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteCond %{HTTP:Host} ^[^.]+\.[a-z]{2,5}$ [NC]
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
get it from other question on stackoverflow :)
But if I use www domain all is ok, if not get this redirect
http://domain.de/www/htdocs/user/_production/http%28?s%29://www.

Why not simplify your rules like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !=on
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]

Related

Redirect all http/www into https://www

I have this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
What this does is:
With http://example.com it redirects to https://www.example.com (this is correct)
With https://example.com it redirects to https://www.example.com (this is correct)
But with http://www.example.com it doesn't redirect to https://www.example.com.
Please take note that It needs to be not more than 1 chain. It should redirect to https://www.example.com.
You can avoid hardcoding your host name and do both conditions in a single redirect rule like this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Okay I just fixed it.
this is the updated code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I can guarantee that this will all do 301 redirect without more than 1 chain.
I hope this helps some developers out there!.

How to exclude subdomains from redirecting?

I have this code to force any URL to be "https://www.":
#rewrite to WWW:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have sub-domains and I do not want any of them to be redirected. What can I do?
You can do it all in single rule:
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(?:static\.|subdomain1\.|subdomain2\.|\d+) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache before testing this rule.

apache .htaccess rewrite should not include www

We have a domain test.com, we want to redirect http://www.test.com to http://test.com and https://www.test.com to https://test.com, We can achieve the same by below mentioned rule, but we dont want http:// and beside what do we have to do for https://
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You can have a single rule to handle both http and https traffic:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L,NE]
If you want everything to go to https, then you need to change your rule to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
Otherwise, you'll need separate rules for http and https:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Redirecting a couple of pages to https from .htaccess file

I want to redirect couple of pages to https. As I've already use one condition to redirect all requests to http, for those coupoe of pages, it shows too many redirects.
Look at my code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php
This doesn't work. "Too many redirects".
Anyone can help?
The first rule makes no sense.
Shorten to this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php [L,R]
If the idea of the first rule was to force www, add this:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess - add www and direct all to root level

I would like all requests to be directed to the root page. I also need www to be added if it has been missed out. I have tried numerous attempts but can not seem to get it to work, latest attempt
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
RewriteRule !^.*/ / [NC,L]
This code below works providing that they do not use www within their requests.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
You can use this single rule as your first rule in your DOCUMENT_ROOT/.htaccess file::
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://www.domain.com/? [L,R=301]