Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite index.php/ problem

rewite index.php with and without trailing slash

         

stein

4:01 pm on May 15, 2008 (gmt 0)

10+ Year Member



Hi,

I have the below htaccess rewrite rule to stop duplicate content on my sites home page.

www.example.com/index.php is rewritten to www.example.com/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

but if I add a slash

www.example.com/index.php/ then it is not rewritten to www.example.com/

could someone please help?

Thanks

[edited by: tedster at 4:03 pm (utc) on May 15, 2008]

[edited by: jdMorgan at 8:14 pm (utc) on May 15, 2008]
[edit reason] switch to example.com - it can never be owned [/edit]

jdMorgan

8:15 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the "?" regular-expressions quantifier to require zero or one trailing slashes in the patterns:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php/?\ HTTP/
RewriteRule ^(.*)index\.php/?$ /$1 [R=301,L]

Jim

stein

9:56 am on May 16, 2008 (gmt 0)

10+ Year Member



Many Thanks

That worked perfectly :)

Conrad

stein

10:15 am on May 16, 2008 (gmt 0)

10+ Year Member



Sorry but now I see another problem.

if I type www.example.com/index.php/anything

then it is not rewritten to www.example.com

How do I ensure that no matter what is written after the index.php/ it is rewritten to www.example.com

Thanks

Conrad

jdMorgan

2:47 pm on May 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just change the rule pattern:

RewriteRule ^(.*)index\.php /$1 [R=301,L]

See the regular-expressions tutorial cited in our Forum Charter for more info.

Jim