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
Related videos on Youtube
Author by
Arash Mousavi
Updated on September 18, 2022Comments
-
Arash Mousavi over 1 year
How can I force
Apache
ifURI
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 sameURL
.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 almost 11 yearsDon't use "THE_REQUEST", it's not unescaped. Why does your rewrite rule start with "my"??
-
Arash Mousavi almost 11 yearsI don't know,a guy suggested it to me. Should I remove it?
-
-
Arash Mousavi almost 11 yearscan you express examples according to my examples?
-
Arash Mousavi almost 11 yearscan you express examples according to my specific patterns?
-
dawud almost 11 yearsNo, that's your work.
-
Arash Mousavi almost 11 yearsyour answer is irrelevant to my question
-
Arash Mousavi almost 11 yearsI questioned about any URL that start with "admin-", but your example is static.
-
Arash Mousavi almost 11 yearsand also I want redirect any url with https without these two pattern redirect to http.
-
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 almost 11 yearsI edit my question
-
Philip almost 11 yearsUsing mod_alias like this can result in a redirect loop. You really should mention that, and how to avoid it.
-
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 almost 7 yearsthat's your work - lol