Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess : redirect invalid page



2:51 pm on Feb 24, 2013 (gmt 0)


I wrote the following htaccess file in order to convert directory-based address to query string :
RewriteEngine on
RewriteBase /

RewriteRule ^$ /index.php?page=main [L]

RewriteRule ^(icons|js|securimage) - [L]

RewriteRule ([a-z]+)/(.+)/(.+)/(.+)\.html /index.php?lang=$1&page=$2&sub_page=$3&param=$4 [QSA,L]
RewriteRule ([a-z]+)/(.+)/(.+)\.html /index.php?lang=$1&page=$2&sub_page=$3 [QSA,L]
RewriteRule ([a-z]+)/(.+)\.html /index.php?lang=$1&page=$2 [QSA,L]

The first rules are there to avoid modification of some adresses.

This htaccess file works properly and I have no problem with it.

Now, I would want that all invalid pages are redirected to /index.php page. So, I completed this htaccess file with the following :
RewriteRule ^(.*)$ /index.php [L]

This results in an error 500 for all pages (including the ones that previously worked.

Could someone help me correct this ?



2:38 am on Feb 25, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

You've created an infinite loop. [L] does not mean "stop right here and proceed directly to the page". It means "go back to the beginning of the list of Apache modules and start over, this time using the 'target' as your new 'pattern'".

Fortunately there are, at a rough estimate, 70,000 earlier posts discussing rewrites, redirects, the role of a RewriteCond, and the appropriate ordering of rules.

Now, about that




8:59 am on Feb 25, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

The (.+) elements should be changed to a more specific pattern. See other discussion about using (.*) elements only a few hours ago. Both are equally bad for the same reasons.

With this rule,
RewriteRule ^(.*)$ /index.php [L]

requests for images and for CSS and JS files are rewritten too. It's a very bad idea as it leads to an infinite loop. You need a preceding RewriteCond to exclude internal requests for index.php from matching again and being rewritten again.

To simplify your site, have you considered extensionless URLs for pages? With this change, URL requests with extensions are served by real files (images, CSS, JS, etc) and extensionless URL requests for pages are served by the index.php file by passing parameters to it in a rewrite.

Non-existing URLs should return 404 status. Usually Apache does that, but your PHP script will need to do that.


4:42 am on Mar 2, 2013 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome to WebmasterWorld, Brice!

have you gotten anywhere with the suggestions provided by lucy24 and g1smd?


9:02 am on Mar 2, 2013 (gmt 0)

Hello Phranque, g1smd and lucy24,

Yes, I have solved my problem : the post of lucy24 helped me understand my problem, and the post of g1smd gave me a simple idea to solve it.

Thanks to all of you.


Featured Threads

Hot Threads This Week

Hot Threads This Month