RGriffiths RGriffiths - 1 year ago 141
Apache Configuration Question

Remove php from url (again!)

I have seen so many posts about this so apologies for yet another. I have tried to read around how this works with little success: To start with here is my .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

The first sets http to https - it works so .htaccess active.
The second is meant to remove the .php from the url - it doesn't.

My main question is how to remove .php from each URL. My second rewrite rule is meant to do this but it doesn't. Yes, it is copied from a previous question but I would really like to understand exactly what the syntax means so that I can understand why it is not working.

So my questions are:

  1. Can anyone point me to a decent explanation of the syntax for
    ? I have looked a number of docs including the Apache http://httpd.apache.org/docs/current/rewrite/ docs but I am no better off.

  2. And / or why does the .php removal not work?

A cheeky extra simple question ... do I need to repeat RewriteEngine On or do I just need it once?

Answer Source

Have it like this (see comments above each rule):

RewriteEngine On

# redirect externally http -> https for non POST data
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# To externally redirect (change URL in browser) /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally (silent) rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download