Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set apache redirect from http to https for a specific url

I would like to redirect any URL path that is having a /test to https://localhost/test. Also, if an url is /test?user=123 it has to be redirected to https://localhost/test?user=123 or rather if the url is /test/test_db/user?id=123&pwd=123 has to be redirected to https://localhost/test/user/test_db/user?id=123&pwd=123

All other requests of any kind has to be redirected to a html page that says "Access denied" in the root folder(http://localhost/accessdenied.html).

How do I achieve this with RedirectMatch in apache. I tried something like

RedirectMatch permanent ^test/(.*)$ https://localhost/test/$1

Which did not work.

like image 953
Abhishek Avatar asked Apr 24 '26 00:04

Abhishek


1 Answers

I wonder if this is better as a ServerFault question.

Anyway:
I don't know how you can achieve this with RedirectMatch, but I do know how you can do this with ModRewrite:

RewriteEngine On$
RewriteCond %{HTTPS} off$
RewriteRule ^/bla https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]$

This is a fairly generic form that'll work for any HTTP Host (as I don't know any details about your host) and will redirect anything that matches with bla at the start of the URL and isn't already Https, with arguments and everything.

like image 198
jonathanl Avatar answered Apr 26 '26 02:04

jonathanl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!