Examples of redirect using Apache rewrite module

The following shows how to redirect from one server to another server with parameters:
RewriteEngine On
RewriteRule ^redirectIfWithTheseCharactersInURL\/?(.*)$
"http\:\/\/another\.server\.com\/redirectIfWithTheseCharactersInURL\/$1" [R=301,L]
The following shows how to redirect the root path to sub directory using Apache redirect
Put the following in your httpd.conf or apache.conf or under your html root folder's .htaccess file.

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using.htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?i88.ca$
RewriteCond %{REQUEST_URI} !^/yourSubDirectory1/yourSubDirectory2/
# The following does not redirect on existing file and directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# It defaults to temporary if you don't specify permanent
RewriteRule ^(.*)$ /yourSubDirectory1/yourSubDirectory2/$1 [R=permanent]
RewriteCond %{HTTP_HOST} ^(www.)?i88.ca$
RewriteRule ^(/)?$ yourSubDirectory1/yourSubDirectory2/index.php [L]

To exclude a sub-directory from redirect, put a RewriteCond before the RewriteRule, for example, to exclude the /wordpress url being redirected:

RewriteCond ...
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteRule ...

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules. More about flages here.



Popular posts from this blog

Check MySQL query history from command line

Installing chocolatey on Windows

Fixed: ImportError: No module named utilities.common.tools