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

Redirect index.php to my domain without looping
im lost on this..

10+ Year Member

Msg#: 4899 posted 7:40 pm on Oct 26, 2005 (gmt 0)


I want to permanently redirect requests to www.mysite.com/index.php to www.mysite.com - I believe i had tried this in the past with:

RedirectPermanent /index.php [mysite.com...]

and this messed up everything, causing an infinite loop.

how can i do this effectively? (right now google lists both as different pages, and i want to avoid the duplicate content penalty..)



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

Msg#: 4899 posted 9:15 pm on Oct 26, 2005 (gmt 0)

The problem with this is that any request for "/" is internally rewritten to index.php by Apache mod_dir if you have index.php in the DirectoryIndex list (the usual set-up), so it's difficult to avoid an infinite loop.

It can be done with mod_rewrite in .htaccess like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

An example of THE_REQUEST looks like this:
GET /index.php HTTP/1.1

It could also be:
HEAD /index.php HTTP/1.0

so a flexible pattern was provided to handle all valid request cases.

THE_REQUEST is the original request from the client (browser or robot). The RewriteCond will only allow the RewriteRule to be invoked if the client actually requested index.php, and not if the current (internal) request is for index.php only as a result of mod_dir's DirectoryIndex internal URL rewrite. While the rule pattern appears to be redundant, it improves the code efficiency because RewriteConds are only processed if the RewriteRule pattern matches.

The first directive may or may not be needed. It may cause a server error if it is needed but missing, or if it not needed but present. This depends on your server, so you may have to test to find out. The first two directives won't be needed if you already have working rewriterules in your .htaccess file.



10+ Year Member

Msg#: 4899 posted 8:09 pm on Oct 27, 2005 (gmt 0)

thank you so much,

this worked great. i was looking for this code for a LONG time. you are the man!

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