jbirch
jbirch

Reputation: 35

Rewrite dynamic urls

I have a user.php file that contains code for different pages - account, data and home. They are currently accessed through a dynamic url with GET parameters like so:

http://localhost/user.php?page=home
http://localhost/user.php?page=account
http://localhost/user.php?page=data

They all work fine and I am happy with how it works. But what I would like is for them to be displayed as http://localhost/user/<thepagename>/, preferably with ?page=home as http://localhost/user/.

I have tried a few .htaccess rules from the web but these either completely don't work, or they require me to go through user.php to get to the rewritten urls, which is just as messy and "ugly" as having the parameters in the url (as in http://localhost/user.php/pagename/).

I know the rewrite is not necessary in this case but I am working on this as a sort of testing project to get to grips with the practical uses for a variety of functions and practices.

Thanks in advance.

Upvotes: 3

Views: 112

Answers (1)

anubhava
anubhava

Reputation: 786349

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^user/?$ /user.php?page=home [L,QSA,NC]

RewriteRule ^user/(.+?)/?$ /user.php?page=$1 [L,QSA,NC,NE]

Upvotes: 3

Related Questions