views:

24

answers:

1

I'm trying to write a simple rule such that:

/page gets implicitly routed to /page.php. Inversely, I'd like any direct access to /page.php to be redirected in the address bar to /page.

Currently I have:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

But that does not handle the 2nd case. How can I modify it?

+2  A: 

You have to treat your second rule before your first one. Try this:

RewriteEngine on
RewriteRule ^(.*)\.php$ http://domain.com/$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php
pixeline
This seems to work except that css/js files (linked to like c/main.css and lib/site.js) are not loaded.
BobbyB
i've updated the code to accommodate for that issue. Basically, i prevent the last rule from running if hte request is a file. Since php files are blocked upstream, they will have already been taken care of and it should work as you expect.
pixeline
Cheers that did the job.
BobbyB