apache .htaccess rewrite should not include www - regex

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]

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.

htaccess rewrite applying to all, not intended

In my root .htaccess I have
Options +FollowSymlinks
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/dashboard/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^dashboard.domain.com
RewriteRule ^(.*)$ /dashboard/$1 [NC,L,NS]
In the dashboard folder's .htaccess I have..
RewriteRule ^go/([^/.]+)/?$ index.php?flag=$1 [L]
When I go to dashboard.domain.com/login it throws up an error.
When I go to dashboard.domain.com/go/a it works.
When I # out the dashboard rewrite rule the opposite occurs, /login works, but then /go/$1 doesn't. What am I missing to make the /go rewrite only apply when /go is detected?
EDIT (rest of root htaccess)
AddHandler application/x-httpd-php54 .php
Options +FollowSymlinks
Options -Indexes
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/rocket/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^launch.domain.com
RewriteRule ^(.*)$ /rocket/$1 [NC,L,NS]
RewriteCond %{REQUEST_URI} !^/dashboard/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^dashboard.domain.com
RewriteRule ^(.*)$ /dashboard/$1 [NC,L,NS]
RewriteCond %{REQUEST_URI} !^/img/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^img.domain.com
RewriteRule ^(.*)$ /img/$1 [NC,L,NS]
#RewriteCond %{HTTPS} off
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC]
#RewriteCond %{HTTP_HOST} !launch.domain.com [NC]
#RewriteRule ^(.*)$ https://launch.domain.com/$1 [R=301,L,QSA]
Redirect 301 /sneakpeek /
RewriteRule ^error/([^/]+) error/failure.php?code=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
<FilesMatch "\.(ttf|otf|eot|woff|jpg|png|jpeg|gif|js|json|html|css|php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
ErrorDocument 400 /error/400
ErrorDocument 401 /error/401
ErrorDocument 403 /error/403
ErrorDocument 404 /error/404
ErrorDocument 500 /error/500
Rules below your dashboard rule might be affecting your rule and changing %{REQUEST_URI} variable. You can use THE_REQUEST variable and retest it after clearing your browser cache:
RewriteCond %{THE_REQUEST} !/(dashboard|error)/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} =dashboard.domain.com
RewriteRule ^(.*)$ dashboard/$1 [NC,L]

htaccess redirect from www to non-www

I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
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]

rewrite url to non www multiple sites

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]