Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 :

http://examplesite.com/admin-2dns24dw 

Redirect to :

https://examplesite.com/admin-2dns24dw 

And if the URI is :

http://examplesite.com/en/admin/user/login/msg

redirect to :

https://examplesite.com/en/admin/user/login/msg

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 :

https://examplesite.com/fa/dashboard

Redirect to :

http://examplesite.com/fa/dashboard

** 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

2 Answers 2

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
8  
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.

Example:

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

* UPDATE *

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