homepage Welcome to WebmasterWorld Guest from 54.227.20.250
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 all /index.php urls to /
redirect index.php
chomp



 
Msg#: 4487862 posted 3:56 am on Aug 24, 2012 (gmt 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]

 

smallcompany

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4487862 posted 4:08 am on Aug 24, 2012 (gmt 0)

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!

chomp



 
Msg#: 4487862 posted 4:57 am on Aug 24, 2012 (gmt 0)

Jackpot! Thanks a lot mate.

g1smd

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



 
Msg#: 4487862 posted 6:29 am on Aug 24, 2012 (gmt 0)

In the original post, in the target URL in rule 2 you do not need the quotes nor the escaping.

lucy24

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



 
Msg#: 4487862 posted 8:49 am on Aug 24, 2012 (gmt 0)

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.

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