Server Fault is a question and answer site for system and network administrators. 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

How can I force Apache if URI start with "admin-" pattern or contain "admin/user/login" redirect to https of the same uri.

For example:

If the URI is : 

Redirect to : 

And if the URI is :

redirect to :

And if any https URI that hasn't any of this patterns should be redirect to http of same URL.

For example:

If the URI is :

Redirect to :

** UPDATE **

I tried:

 RewriteCond %{HTTPS} !=on
 RewriteCond %{THE_REQUEST} admin-|admin/users/login [NC]
 RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

but it didn't worked.

whats the problem?

share|improve this question

marked as duplicate by Chris S Jul 30 '13 at 17:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Don't use "THE_REQUEST", it's not unescaped. Why does your rewrite rule start with "my"?? – Chris S Jul 30 '13 at 17:21
I don't know,a guy suggested it to me. Should I remove it? – Arash Mousavi Jul 30 '13 at 17:23

This is very simple using mod_rewrite:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(foo|bar)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

This will rewrite any URL that matches the pattern in the RewriteRule to the same URL over HTTPS.

share|improve this answer
can you express examples according to my specific patterns? – Arash Mousavi Jul 30 '13 at 16:58
No, that's your work. – dawud Jul 30 '13 at 16:59
your answer is irrelevant to my question – Arash Mousavi Jul 30 '13 at 17:00
@ArashMousavi The answer is absolutely relevant, as is the question this has been marked as a duplicate of. Stack Exchange is not here to do your job for you. – voretaq7 Jul 30 '13 at 17:10
I edit my question. – Arash Mousavi Jul 30 '13 at 17:12

You can try mod_alias - Apache HTTP Server Redirect Directive.


Redirect /service 


Redirect /admin-2dns24dw
share|improve this answer
can you express examples according to my examples? – Arash Mousavi Jul 30 '13 at 16:57
I questioned about any URL that start with "admin-", but your example is static. – Arash Mousavi Jul 30 '13 at 17:04
and also I want redirect any url with https without these two pattern redirect to http. – Arash Mousavi Jul 30 '13 at 17:05
I edit my question – Arash Mousavi Jul 30 '13 at 17:13
Using mod_alias like this can result in a redirect loop. You really should mention that, and how to avoid it. – Chris S Jul 30 '13 at 17:20

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