Force https using . htaccess on a given domain and domain only

Asked

Viewed 1,424 times

1

I would like to know how to force HTTPS using .htaccess in only one domain/domain. The code I use is forcing https on all subdomains... and one of my subdominios does not have SSL, so I wish it does not work on https.

Man .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I’ve tried to put a different htacess in my domain, but it didn’t work.

  • @Danielomine on this topic you indicated he teaches how to activate https in just one domain, without affecting others. What I need is for https to work on meusite.com and shop.meusite.com without affecting others.

  • 1

    The correct name for the file is: . htaccess and not htacess, could check if your file is with the correct name?

  • 1

    @Allanandrade I discovered the problem, my browser saved the cache with the previous htacess configuration and for some reason was not updating with the new htacess I set up in the subdomain... after I cleaned the cache worked normally...

  • on the link, follows the same logic.. just understand what has been done and apply to your case.

1 answer

3


The correct name for the file is: .htaccess

Use mod_rewrite to force HTTPS is not recommended for security reasons. read more on Redirectssl

It is recommended that you use Virtual Host for this... you can try this:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://secure.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName secure.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

If even knowing this, still want to use mod_rewrite:

1 = Remove the file .htaccess root site: usually /var/www/html

2 = Create virtual hosts.

3 = Place a .htaccess at the web root of every virtual host you want HTTPS.

File example .htaccess: Rewriteengine On # This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

For other users who demand on the same issue, remember to check if your webserver is with the REWRITE module enabled, if not, you can enable with the command:

a2enmod rewrite

I hope I’ve helped!

  • Hi @Allan Andrade. From what I understand this virtual host is a configuration on my server, and my host is super limited and I doubt it will allow me to make such a configuration, now about mod_rewrite what I need is for it to configure a domain and Subdomain on the same htacess to stay on https, the rest should be http, as I specified in my question. I know how to activate https for just one domain, or for all, but I don’t know how to remove the others from this condition.

  • 1

    Are these domains in the same folder? Same files? Same code?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.