Ben Ben - 1 year ago 78
MySQL Question

Remove .php from url WITH exceptions (htaccess)

I have the following .htaccess file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1

This basically serves my user profiles so like twitter someone can just type:

However I want to make certain pages like /home not lead to a profile but a different .php page, again without the .php at the end.

Can anyone think of a way to do this?


Answer Source

You can have another rule to add .php before this rule:

RewriteEngine On

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

# handle profile URLs    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ account.php?username=$1 [L,QSA]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download