Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've heard that you can use mod_rewrite to clean up your URLs, and basically I'd like to make it so the .php file extension isn't shown in the URL.

For example, let's say I have the following file structure on a site:


I would like to make the following basic changes with mod_rewrite:

  • When a request is made to the root i.e. /about, if a directory is found called about, then it should behave as a directory nomrally does (i.e. load the index.php from the direcotry).
  • If a directory is not found, it should point to about.php
  • If a request for a .php file is made, it should 301 redirect to the filename without the .php extension.
  • Only the directories which would actually be viewed directly in the browser should have these URL changes (i.e. the root and the admin directory), therefore css and img should have their URLs unchanged.

How can I make those changes using mod_rewrite?

share|improve this question

In .htaccess file:

RewriteEngine On
RewriteBase /

# Abort on directories, skip sub-requests
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [L,NS]

# Check if there is php file (add extension)
RewriteCond %{REQUEST_URI}\.php -f
RewriteRule (^.*) $1\.php [L]

#Redirect explicit php requests (if file exist)
RewriteCond %{REQUEST_FILENAME} -f
# strange - REQUEST_URI always contains .php
# have to parse whole request line
# look for .php in URI (fields are space separated) but before '?' (query string)
RewriteCond %{THE_REQUEST} ^[^\ ]+\ /[^\ \?]*\.php
RewriteRule (.*)\.php$ $1 [NS,R=301]

mod_rewrite is extremely powerful :)

share|improve this answer
Oops, I missed the options in RewriteCond. Love how powerful Apache is. – couling Feb 13 '12 at 9:40
It's so powerfull! You've just discovered the top of the iceberg! – Olivier Pons Mar 23 '12 at 8:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.