Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration file.


/etc/httpd/conf/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"
Redirect /service http://www.joymaininc.com/new
</VirtualHost>

<VirtualHost *:80>
ServerName postbox.joymaininc.com
ServerAlias www.joymaininc.com
DocumentRoot "/var/www/joymaininc"
</VirtualHost>

 

http://www.example.com/service will be redirected to http://www.joymaininc.com/new

The URL must have "service" string to activate the Redirect directive.


NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"
Redirect / http://www.joymaininc.com/
</VirtualHost>

<VirtualHost *:80>
ServerName postbox.joymaininc.com
ServerAlias www.joymaininc.com
DocumentRoot "/var/www/joymaininc"
</VirtualHost>

http://www.example.com will be redirected to http://www.joymaininc.com

http://www.example.com/service will be redirected to http://www.joymaininc.com/service


<VirtualHost 66.199.140.191:80>
ServerName vanarts.com
ServerAlias www.vanarts.com
DocumentRoot /home/vanarts/public_html
ServerAdmin kaiming@vanarts.com
Redirect /webmail https://mail.vanarts.com/owa


</VirtualHost>

ScriptAliasMatch ^/?webmail/?$ /usr/local/cpanel/cgi-sys/wredirect.cgi

 

http://www.vanarts.com/webmail will be redirected to https://mail.vanarts.com/owa even though there is "ScriptAliasMatch ^/?webmail/?$ defined.


RedirectMatch Directive

The RedirectMatch directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching. The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into given string and use it as a filename.

RedirectMatch (.*)\.jpg$ http://www.joymaininc.com/$1.jpg

When you input http://localhost/test/graph1.jpg, the Apache server will send http://www.joymaininc.com/test/graph1.jpg back to browser.
When you input http://localhost/graph1.jpg, the Apache server will send http://www.joymaininc.com/graph1.jpg to browser.


RewriteRule Directive

What is matched?

The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"

RewriteEngine On
RewriteRule ^/other(.*)$ /some$1

</VirtualHost>

mkdir /var/www/example/other

mkdir /var/www/example/some

create a test.html file in /var/www/example/other directory

create a test.html file with different content in /var/www/example/some directory

/usr/sbin/apachectl restart

http://191.121.13.5/other/test.html

o

Even though the content is from /some/test.html, the URL still shows /other/test.html.


httpd.conf

Listen 80
Listen 2082

/usr/sbin/apachectl start

Apache (13)Permission denied: make_sock: could not bind to address::2082

system-config-selinux

Network Port-->Add
Type:http_port_t

a

httpd.conf

Listen 80
Listen 2082

NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"

RewriteEngine On
RewriteRule ^/(.*) http://191.121.13.5:2082/a$1 [P]

</VirtualHost>

<VirtualHost *:2082>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"
</VirtualHost>


With [P] flag, you must make sure that the substitution string is a valid URI (typically starting with http://hostname) which can be handled by the Apache proxy module.

mkdir /var/www/example/asome

mkdir /var/www/example/aother

Create a new test.html and put it in /var/www/example/asome directory and /var/www/example/aother directory.

When accessing http://191.121.13.5/some/test.html,

1


httpd.conf

Listen 80

Listen 2082

NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example"

RewriteEngine On
RewriteRule ^/webmail(.*)$ http://191.121.13.5:2082/webmail [P]

</VirtualHost>

<VirtualHost *:2082>
ServerName postbox.joymaininc.com
ServerAlias www.joymaininc.com
DocumentRoot "/var/www/joymaininc"
</VirtualHost>


When accessing http://191.121.13.5/webmail or http://191.121.13.5/webmail/, it will be redirected to http://191.121.13.5:2082/webmail/.

If you define

RewriteRule ^/webmail/?$ http://191.121.13.5:2082/webmail [P]

http://191.121.13.5/webmail/ will be redirected to http://191.121.13.5:2082/webmail/. But http://191.121.13.5/webmail will not be redirected.


RewriteEngine On

RewriteCond %{HTTP_POST} ^webmail\.
RewriteRule ^/(.*) http://191.121.13.5:2082/$1 [P]

Both http://191.121.13.5/webmail and http://191.121.13.5/webmail/ are redirected to http://191.121.13.5:2082/webmail/


RewriteRule vs. Redirect

RewriteRule is server-side and Redirect is client-side.