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

  • When you say "doesn't work", what actually happens? No redirection? Redirect loop? What is the /code part for in the middle step?
    – MrWhite
    Jan 20, 2013 at 16:48

2 Answers 2


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]

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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