Folkmann
Folkmann

Reputation: 6341

htaccess rewrite and remove .php extension

So, I have been reading up on mod_rewrite because I want to remove the .php extension and have the URL look different, so that the end user doesn't see all the arguments in the URL.

My goal: /foo.php?p=bar Looks like: /foo/bar

Where foo and bar can be anything, numeric and chars. I'm only working with .php files, so it doesn't have to work with .html and alike.

Upvotes: 1

Views: 80

Answers (1)

anubhava
anubhava

Reputation: 786291

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?p=$2 [L]

Upvotes: 1

Related Questions