Welcome to WebmasterWorld Guest from 107.20.54.98

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect all /index.php urls to /

redirect index.php

     
3:56 am on Aug 24, 2012 (gmt 0)

New User

joined:Aug 24, 2012
posts: 2
votes: 0


Hi,

I am trying to redirect all index.php urls site wide to the url without index.php to prevent duplicate content but can only seem to redirect the homepage url and not the internal urls. If someone is able point out where I am going wrong that would be great.

This is what my .htaccess file currently looks like:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/
RewriteRule ^index\.php(/(.*))?$ http://www.example.com/$2 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\.au$
RewriteRule ^(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]

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

[edited by: incrediBILL at 9:32 am (utc) on Aug 24, 2012]
[edit reason] unlinked URLs in code [/edit]

4:08 am on Aug 24, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 2, 2006
posts:2112
votes: 2


I have this from this same forum, some time ago:

# redirect all <subdirectory>/index.html requests to <subdirectory>/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1? [R=301,L]

I made same question like you, except it was for HTML vs. your PHP which does not really make any difference. The code was written by a guru that mastered this thread in the past (jdMorgan), so I doubt it can get any better.

In your case, I believe it would look like this:

# redirect all <subdirectory>/index.php requests to <subdirectory>/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1? [R=301,L]

Certainly, replace the "example.com" with your URL. Test it. It works with home page and subdirectories as well.

Cheers!
4:57 am on Aug 24, 2012 (gmt 0)

New User

joined:Aug 24, 2012
posts: 2
votes: 0


Jackpot! Thanks a lot mate.
6:29 am on Aug 24, 2012 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


In the original post, in the target URL in rule 2 you do not need the quotes nor the escaping.
8:49 am on Aug 24, 2012 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:12696
votes: 244


RewriteRule ^index\.php(/(.*))?$ http://www.example.com/$2 [R=301,L]

What would you expect to find after "index.php"? (Please don't say "a query string". It's almost 2AM and I can't deal with it.)

This Rule works only on the top-level site index, because you've put your opening anchor right in front of the "index.php" both in the Rule itself and in the Condition. Did your cat walk across the keyboard? Somehow the pattern seems to have got turned backward.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members