Menu

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/mod_rewrite.so


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 http://example.com/foo to the path /usr/local/apache2/htdocs/bar.


An absolute URL

  • RewriteRule "^/product/view$"  "http://site2.example.com/seeproduct.html" [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  "(.*)"  http://intranet.example.com%241/



Examples:

Redirect All Website Pages

# Redirect all pages from olddomain.com # to newdomain.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$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=http://www.example.com/" />

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   http://example.com/newpage1.html
Redirect permanent   /folder/page2.html  http://example.com/newpage2.html

Reference link:



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

<If "%{SERVER_PROTOCOL} != 'HTTPS'">
    Redirect "/admin/" "https://www.example.com/admin/"
</If>



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

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ "http://example.com/$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:

<?php
Header('Location: http://example.com/newpage',TRUE,301);
?>

Reference link:



Rewriting from Old to New (external):
Solution:
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>
  ServerName www.example.com
  ServerAlias example.com www.new-example.com

  Redirect "/" "https://www.example.com/"
</VirtualHost>

 OR

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

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} !^vhost2.test.com$
        RewriteRule ^/(.*) http://%{HTTP_HOST}/$1 [L,R]
    </IfModule>



Redirect a URL to www.example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]



Redirect request from url https://10.20.40.50 to https://www.domain.example.com.

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



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 http://example.com/image/foo.gif would cause the server to return the file /ftp/pub/image/foo.gif.

Reference link:



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

Example:
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