1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Alexander_F
  • 2,831
  • 3
  • 28
  • 61

1 Answers1

1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • get server error... not clear what the problem is in this to blocks 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] – Alexander_F Feb 01 '12 at 14:58
  • Actually my bad, there was a space character before `%{REQUEST_URI}`. I edited it, pls try it again. – anubhava Feb 01 '12 at 15:06
  • great, thank you, how can I exclude some subdomains to be redirected? like admin.domain.com – Alexander_F Feb 01 '12 at 18:45
  • 1
    You're welcome. You can add a RewriteCond line like this: `RewriteCond %{HTTP_HOST} !^admin\.domain\.com$ [NC]` to avoid redirection for `admin.domain.com` – anubhava Feb 01 '12 at 18:51