Tuesday 12 February 2019

Apache Rewrite Rules

  • Apache provides a REWRITE RULE function to redirect the requests from one URL/Page to another URL/Page.
  • To use this functionality we have to enable the mod_rewrite module in apache web server.
  • mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. 
  • mod_rewrite operates on the full URL path, including the path-info section. A rewrite rule can be invoked in httpd.conf or in .htaccess file.

Enable module:
  • LoadModule rewrite_module modules/

NOTE: We should not always use .htaccess file for large number of rewrite rule redirection, since which may leads to the slowness of Apache server.

#####Forward/Rewrite , main domain towards application context.

RewriteEngine on
RewriteRule   ^/$     /CONTEXT/ [R]

RewriteRule Basics
A RewriteRule consists of three arguments separated by spaces. The arguments are
  • Pattern: which incoming URLs should be affected by the rule;
  • Substitution: where should the matching requests be sent;
  • [flags]: options affecting the rewritten request.

The Substitution can itself be one of three things:

A full filesystem path to a resource
  • RewriteRule "^/games"  "/usr/local/games/web"

This maps a request to an arbitrary location on your filesystem, much like the Alias directive.

A web-path to a resource
  • RewriteRule "^/foo$"  "/bar"

If DocumentRoot is set to /usr/local/apache2/htdocs, then this directive would map requests for to the path /usr/local/apache2/htdocs/bar.

An absolute URL

  • RewriteRule "^/product/view$"  "" [R]

Rewrite Conditions

For example, to send all requests from a particular IP range to a different server, you could use:

RewriteCond  "%{REMOTE_ADDR}"  "^10\.2\."
RewriteRule  "(.*)"


Redirect All Website Pages

# Redirect all pages from # to
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,L]

Meta Refreshes:

This method uses a special Meta tag in the HTML source to control the redirect. In the early days of the Internet, this was the main method of generating redirects.
The meta refresh should appear within the head section of the HTML source.

An example is:

<meta http-equiv="Refresh" content="0; url=" />

The number (0 in this example) refers the the number of seconds to wait before performing the redirect. A zero second delay results in an instant redirect and is the equivalent of doing a 301 permanent redirect.

Reference link:

Redirect rule for old page to new page:

RewriteEngine On
Redirect permanent   /page1.html
Redirect permanent   /folder/page2.html

Reference link:

 Redirect a portion of your site to HTTPS, you might do the following:

    Redirect "/admin/" ""

Redirect all requests from the www to the non-www version of the domain, or vice-versa:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$ "$1" [R=301,L]

Reference link:

PHP Redirects :
If you want to do a PHP 301 permanent redirect, you just need to add the redirect code, ie:


Reference link:

Rewriting from Old to New (external):
We force a HTTP redirect to the new URL which leads to a change of the browsers and thus the users view:

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "bar.html"  [R]

Reference link:

Redirect all pages from Http to Https:

<VirtualHost *:80>

  Redirect "/" ""


RewriteEngine On
RewriteRule ^/$  https://%{HTTP_HOST}/ [R,L]
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R,L]

RewriteEngine On
RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L

Redirect all pages from Https to Http:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{SERVER_PORT} ^443$ [OR]
        RewriteCond %{HTTPS} =on
        RewriteCond %{HTTP_HOST} !^$
        RewriteRule ^/(.*) http://%{HTTP_HOST}/$1 [L,R]

Redirect a URL to

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$1 [R=301,L]

Redirect request from url to

<VirtualHost _default_:443>
RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$1 [L,R]

Alias & AliasMatch  Directive:

The Alias directive allows documents to be stored in the local filesystem other than under the DocumentRoot.

Alias "/image" "/ftp/pub/image"
Alias "/icons/" "/usr/local/apache/icons/"

Note that if you include a trailing / on the URL-path then the server will require a trailing / in order to expand the alias

A request for would cause the server to return the file /ftp/pub/image/foo.gif.

Reference link:

mod_rewrite offers detailed logging of its actions at the trace1 to trace8 log levels.

LogLevel alert rewrite:trace3

To check the logs :
tail -f error_log|fgrep '[rewrite:'

Redirect Detective is a free redirect checker

Reference links:

Thanks :-)

No comments:

Post a Comment