I would like to modify my htaccess so:
http:// domain.com/REQUEST is redirected to https:// domain.com/REQUEST
but
http:// blog.domain.com/REQUEST still goes to http:// blog.domain.com/REQUEST
and
https:// blog.domain.com/REQUEST is redirected to http:// blog.domain.com/REQUEST
how the code should look like?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]
This is assuming main domain and sub domains are all pointed to same DocumentRoot.
Related
I have some unused subdomains and I would like to redirect them to my homepage.
I have found this .htaccess code
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]
It redirects when you call the exact subdomain, for example: test.example.com
But it does not work if you call anything else, for example: test.example.com/meh
It's possibile to redirect any URL in these subdmoains to the homepage?
You may use this rule:
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
%{REQUEST_URI}at the end of target will add previous URI to new target.
i've a htaccess that redirect all pages into https, but how to make ads.php file to exclude from this?
i've try code like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(/ads.php.*)\s [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but still redirecting into https.
so basicly i need to redirect all hxxp://sub.domain.com -> https://sub.domain.com,
but for hxxp://sub.domain.com/ads.php?var=xxxxx aren't getting redirected into https.
Try this RewriteCond in your rule
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ads\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
to exclude ads.php
So i need very simple codes for .htaccess
I want to redirect all traffic to https in main domain and redirect all traffic to http in subdomain without WWW.
Right now i am using this code to redirect all traffic to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So main domain should be like this: https://website.com
and subdomain should be like this: http://sub.website.com
I found many answers but very complicated, so need simple solution.
EDITED
Solution given by anubhava works great. Thanks for your solution.
You can use:
RewriteEngine On
# maindomain -> https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
# subdomain -> http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.website\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R,L]
I've been trying to configure this for my website, but not being able to.
I use to have a cond on my .htaccess to force www for the main domain and nothing for subdomains, but since I got a SSL, I'm having some problems.
It's a wildcard SSL.
What I need is force HTTPS:// WWW on the main domain, and HTTPS:// on subdomains.
I.E: http://www.domain.com -> https://subdomain.domain.com
Is there any rule for that?
Thanks!
EDITED
Now I'm using like Jon posted
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [NC]
RewriteRule ^.*$ https://www.domain.com.br%{REQUEST_URI} [R,L]
# ---------- #
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
The thing is, when on main domain if I type HTTP:// with or without WWW, ir forces HTTPS:// and WWW, that's ok...
But on subdomain, when I type HTTP it doesn't force HTTPS, it redirects to the main domain only... that does not happen if I put a .htaccess inside the dir of the subdomain.
With a .htaccess inside my subdomain dir, if I type HTTP, it forces HTTPS normally...
Any suggestions?
Try:
RewriteEngine On
# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]
Need to redirect http:// www.old-url.com/any/other/url/segment
to https:/ /www.new-url.com/any/other/url/segment
Currently Im doing something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !http://www.old-url.com/1$ [NC]
RewriteRule ^(.*)$ https://www.new-url.com/$1 [L,R=301]
this results:
http:// www.old-url.com/any/other/url/segment to
https:// www.new-url.com/
Thanks
-Robbie
Use below htaccess rule to redirect http to https:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Source: htaccess redirect