Mobile app version of vmapp.org
Login or Join
Barnes591

: Restrict SSL access for some paths on a apache2 server I wanted to allow access to www.mydomain.com/login through ssl only. E.g.: Whenever someone accessed http://www.mydomain.com/login, I wanted

@Barnes591

Posted in: #Apache #Https

I wanted to allow access to mydomain.com/login through ssl only.
E.g.: Whenever someone accessed www.mydomain.com/login, I wanted him to be redirect to www.mydomain.com/login so it's impossible for him/her to access that site without SSL.

I accomplished this by adding the following lines to the virtual host for mydomain.com on port 80 in /etc/apache2/sites-available/default:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/login(.*)$ %{SERVER_NAME}/login [L,R]
RewriteLog "/var/log/apache2/rewrite.log"


Now, I want to restrict using SSL for mydomain.com. That means, whenever someone accessed www.mydomain.com, I want him to be redirected to www.mydomain.com (for performance reasons).

I tried this by adding the following lines to the virtual host of mydomain.com on port 443 in /etc/apache2/sites-available/default-ssl:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^/(.*)$ %{SERVER_NAME}/ [L,R]
RewriteLog "/var/log/apache2/rewrite.log"


But when I now try to access mydomain.com/login, I get an error message that the server has caused to many redirects.
That does make sense. Obviously, the two RewriteRules are playing ping-pong against each other.

How could I work around this?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Barnes591

1 Comments

Sorted by latest first Latest Oldest Best

 

@Welton855

How about:

RewriteCond ^(login) [NC]
RewriteRule ^(.*)$ %{SERVER_NAME}/
RewriteCond !^(login) [NC]
RewriteRule ^(.*)$ %{SERVER_NAME}/ [L]


I hope(!) that works - put it in .htacess or, if using vhosts, the first part goes in the normal vhost and the second bit goes in the ssl vhost.

No need to specify 80/443 to pattern match (LOGIN or login) in the URL and force to the SSL (or not if there is no match).

Okay, did not work

Added bit:

RedirectMatch (?i)/login www.example.com/login

In practice (depends on CMS) all your site links will be http anyway so you should only need the one RedirectMatch.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme