Randy Randy - 1 year ago 105
PHP Question

PHP Pretty URLs combination of two mod_rewrite rules?

I have this .htaccess code in the root folder to replace this url:

to become

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1

This is working fine but what I want is to include subpages like for example the about page to be
instead of
. And the subpages are in the root folder too.

How can I achieve these two rewrites?

Answer Source

You can have this code in mysite/.htaccess:

RewriteEngine On 
RewriteBase /mysite/

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

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