A Typical Case - mod_rewrite: No More Endless Loops!

Started by sukishan, Aug 23, 2009, 12:48 AM

previous topic - next topic
Go Down


A Typical Case
Let's start with an example.

We want to redirect all URIs to a PHP script. The redirection will be internal, so the client, for example, a browser, will not see that a redirection has occurred. To do this, we create in the document root an .htaccess file that contains these rules:

# Enable rewrite engine
Options +FollowSymLinks
RewriteEngine On

# Redirect internally all URIs to /index.php
RewriteRule .* /index.php [L]

However, this will not work - it'll freeze your Apache Web server. Why?

The problem is this line:

RewriteRule .* /index.php [L]

Simply put, that rule 'says' "Redirect all requests to /index.php.". That may sound like what we originally set out to achieve, but, in practice, we will not get the results we expect.

The following is what happens with a request for www.example.net/path/to/:

Phase A

The URI is /path/to/
RewriteRule will rewrite the URI from path/to/ to /index.php
An internal redirection will be made for /index.php
The internal redirection will be treated as a new request, so it will be parsed again (see phase b)

Phase B

The previous internal redirection, /index.php, will be processed again by the rewrite engine
RewriteRule will rewrite index.php to /index.php
An internal redirection will be made for /index.php

At this point the rewrite engine will run Phase B continuously and the Apache Web server will freeze.
A good beginning makes a good ending

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary