3

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.

Abhishek
  • 6,862
  • 22
  • 62
  • 79

2 Answers2

1

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.

jonathanl
  • 524
  • 2
  • 9
  • This did the trick for me, and it answers the question. I think this should be marked as accepted by @Abhishek even if its over a year old. – ahpoblete May 02 '13 at 19:55
  • Don't add the $ at the end of line for me `RewriteRule ^/yourpath https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]` – K. Stopa Nov 07 '19 at 09:14
0

This works just fine for me on my Apache httpd 2.2 server:

RedirectMatch 301 ^(/someprefix[^/]*/.*)$ https://hostname$1
Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77