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?

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.


You can try mod_alias - Apache HTTP Server Redirect Directive.


Redirect /service http://foo2.example.com/service 


Redirect /admin-2dns24dw https://examplesite.com/fa/dashboard
  • 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
  • 1
    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.