apache force if url has specific pattern redirect to https

10,554

Solution 1

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.

Solution 2

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:
10,554

Related videos on Youtube

Arash Mousavi
Author by

Arash Mousavi

Updated on September 18, 2022

Comments

  • Arash Mousavi
    Arash Mousavi over 1 year

    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?

    • Philip
      Philip almost 11 years
      Don't use "THE_REQUEST", it's not unescaped. Why does your rewrite rule start with "my"??
    • Arash Mousavi
      Arash Mousavi almost 11 years
      I don't know,a guy suggested it to me. Should I remove it?
  • Arash Mousavi
    Arash Mousavi almost 11 years
    can you express examples according to my examples?
  • Arash Mousavi
    Arash Mousavi almost 11 years
    can you express examples according to my specific patterns?
  • dawud
    dawud almost 11 years
    No, that's your work.
  • Arash Mousavi
    Arash Mousavi almost 11 years
    your answer is irrelevant to my question
  • Arash Mousavi
    Arash Mousavi almost 11 years
    I questioned about any URL that start with "admin-", but your example is static.
  • Arash Mousavi
    Arash Mousavi almost 11 years
    and also I want redirect any url with https without these two pattern redirect to http.
  • voretaq7
    voretaq7 almost 11 years
    @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.
  • Arash Mousavi
    Arash Mousavi almost 11 years
    I edit my question
  • Philip
    Philip almost 11 years
    Using mod_alias like this can result in a redirect loop. You really should mention that, and how to avoid it.
  • alexus
    alexus almost 11 years
    @ArashMousavi 1) make sure that mod_rewrite isn't commented out 2) look in your logs to see where the problem is, also if you need to use regex pattern then you should use mod_rewrite, if it's simple redirect then mod_alias is better.
  • byronyasgur
    byronyasgur almost 7 years
    that's your work - lol