3

I'm trying to create a Redirect using Apache HTTP Server's mod_alias and core on my system:

# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.1 (Maipo) # rpm -q httpd httpd-2.4.6-31.el7_1.1.x86_64 # 

requirement is to redirect all requests, except for request to /server-status

# cat /etc/httpd/conf.d/_default.conf <VirtualHost *:80> ServerName _default_ <LocationMatch "^/!(server-status)(.*)?"> Redirect / http://X/ </LocationMatch> </VirtualHost> # 

I believe my issue is somewhere with regex, as I'm getting 404 no matter what URL I hit.

2
  • by the way you dont have DocumentRoot in your virtual host, did you added it somewhere else ? servername should be your comp name or domaine name is it normal you set default ? Commented Nov 3, 2015 at 14:19
  • @Froggiz this is catch all VirtualHost, hence ServerName set to _default_ and no DocumentRoot either. btw I'm about to try your answer) Commented Nov 3, 2015 at 14:22

1 Answer 1

4

1 - You can do it using mod rewrite https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

<VirtualHost *:80> ServerName _default_ RewriteCond %{REQUEST_URI} !^/server-status RewriteRule (.*) http://X$1 [L,R=301] </VirtualHost> 

2 - To use Mod_Alias you need RedirectMatch http://httpd.apache.org/docs/current/mod/mod_alias.html

<VirtualHost *:80> ServerName _default_ RedirectMatch 301 ^/(?!server-status)(.*) http://X/$1 </VirtualHost> 

3 - more info:

  • once configuration has been changed, Apache needs to be restarted
  • the server needs to be different else you will have a redirect loop

4 - Bonus

(.*) = catch all in regexp

$1 = result var

R = redirect status code, here you have the list:

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

L = flag which mean Last, here you have the flag list codes:

https://httpd.apache.org/docs/2.4/rewrite/flags.html

5 - Even more... if you really want to use LocationMatch syntax is :

<VirtualHost *:80> ServerName _default_ <LocationMatch "^/(?!server-status)(.*)"> Redirect / http://X/ </LocationMatch> </VirtualHost> 
2
  • I end up using RedirectMatch, everything works) Thanks! Commented Nov 3, 2015 at 14:58
  • i am happy it worked for you ^_^ Commented Nov 3, 2015 at 15:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.