Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having some issues with htaccess rewrites i need to redirect different parts of my site to HTTPS rather than HTTP, for example login and news sections. I'm not sure how to go about doing this in the correct manner.

So at the moment I have

RewriteRule   ^login/?$  index.php?p=login [R,L]

I would like to redirect this to HTTPS, I have tried the following:

RewriteRule ^login/?$ https://%{SERVER_NAME}/index.php?p=login [R,L]

but it doesn't seem to work. Am I missing something? I have rewrite on and all other settings as they work.

Also is there anyway to get htaccess to use a different set of rules if HTTPS is set to when it is not?


So what I want to do is rewrite http://www.site.tld/login/ rewrite to https://www.site.tld/login/code and then further rewrite to https://www.site.tld/index.php?p=login. So the user sees: https://www.site.tld/login/ but it is actually doing https://www.site.tld/index.php?p=login

share|improve this question
When you say "doesn't work", what actually happens? No redirection? Redirect loop? What is the /code part for in the middle step? – w3dk Jan 20 '13 at 16:48

First, redirect /login to https /login/code

RewriteRule ^login/?$ https://%{HTTP_HOST}/login/code  [R=301,L]

You can also force /login/code to redirect to https in case the type in http /login/code:

RewriteCond %{HTTPS} ^off$
RewriteRule ^login/code$ https://%{HTTP_HOST}/login/code  [R=301,L]

Then, you can rewrite /login/code

RewriteRule ^login/code$ /index.php?p=login [R,L]
share|improve this answer

Not sure how to do it all with re-writes. I have the following in the header of my php files:

  if( $_SERVER['SERVER_PORT'] == 80 && $_SERVER['HTTP_HOST']!="localhost") 
   header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

And then this in my .htaccess

#re-write /login/username to /login.htm?login=username
RewriteRule ^/?login/([^/\.]+)/?$ /login.htm?login=$1 [NC,L]
#re-write /login and /login/ account-login.htm
RewriteRule ^/?login/?$ /account-login.htm [NC,L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.