homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess : redirect invalid page

Msg#: 4548356 posted 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 ?




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

Msg#: 4548356 posted 2:38 am on Feb 25, 2013 (gmt 0)

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




WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4548356 posted 8:59 am on Feb 25, 2013 (gmt 0)

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.


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

Msg#: 4548356 posted 4:42 am on Mar 2, 2013 (gmt 0)

welcome to WebmasterWorld, Brice!

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


Msg#: 4548356 posted 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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved