IndianWebPortal.com

Something Different In Linux

Home » htaccess » What is Mod-Rewrite Module

What is Mod-Rewrite Module

Posted by Rishikesh Vispute Categories: htaccess, Knowledge
What is Mod-Rewrite Module?

Mod_rewrite is merely the Apache module that contains the rewrite engine – other servers will process URL rewriting differently.Mod-Rewrite module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism.

The RewriteEngine directive enables or disables the runtime rewriting engine. We need to tell Apache where and what to rewrite. You have two options here – more commonly, you can place the code in a .htaccess file. This is nothing more than a text file which when present in a directory will be interpreted by Apache.The other alternative is only available if you have root access and that is to place the code inside your httpd.conf

How can I enable Mod-Rewrite Module?

Before we can rewrite, there is one option we must first set: FollowSymLinks. This is a security feature of the rewrite engine and you will not be able to rewrite without this option. In most cases, this will already be set in the server httpd.conf but you can safely state it again.Additionally, if you use indexes, we must enable the Indexes option.

In order to enable Mod_Rewrite for your site you should create a text file called .htacccess in the directory where you wish the rewrite rules to apply.
================================================== ==========
#The first three lines of this text file should be:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
#After that you can place your rewrite rules
================================================== ===========

RewriteRule example :=

1] When you want Register a Domain, Find Hosting and Create a Website at Domain.com to be redirected to Register a Domain, Find Hosting and Create a Website at Domain.com, add the following in your .htaccess.

Quote:
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

2] If http://example.com/foo/bar does not exist, redirect to http://other.example.com/foo/bar. (Put this in an .htaccess file in your top-level web directory.)

Quote:
# .htaccess in root of example.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://other.example.com/$1 [R]

3] Handle all requests for top-level .html files and files with no extensions (http://example.com/foo, http://example.com/foo.html) with a single PHP program /foo/show.php. Also, ignore trailing characters in set { : ; , . } so URLs like “http://example.com/foo.” can be copied-n-pasted from plain text sentences by inattentive users.

Quote:
# .htaccess in root of example.com
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /foo/show.php [L,NS]

4]Here are some examples for Joomla’s SEF option:

Quote:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC]
RewriteRule ^(content/|component/) index.php

5]Redirect GET requests for http://example.com/foo/bar to http://example.com/bar (and /foo/bar.html to /bar.html). Handle POST requests with PHP program rather than attempting to redirect a POST (which is unlikely to work well).

Quote:
# .htaccess in foo folder in example.com’s document root
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /$1 [R,L,NS]
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /foo/show.php [L,NS]

Popular Posts

remove index.php fro

How to remove index.php from url in magento Ans : If you ...

PHP-Java bridge on p

  What is php-java bridge? The php-java bridge is the php ...

Set expiration for y

If you want to set the expiration to your site ...

Warning: Parameter 1

Error: Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a ...

domain pointing to e

If you domain uses external MX record and it is ...