Reputation: 143
I've recently started a new site, and for the sake of keeping links clean I want to rewrite urls.
I need to rewrite the links like this :
www.domain.com/post.php?post=15
↓
www.domain.com/post/15
and
www.domain.com/user.php?user=USERNAME
↓
www.domain.com/user/USERNAME
I used to change links from www.domain.com/page.php?page=PAGE to www.domain.com/PAGE , but this time I need both "post" and "user" to work fine, and I can't quite figure it out.
Can someone please help me out with a tip on how to do this?
Thank you in advance
Chris
Solved with Jon Lin's help. His answer works great.
Upvotes: 0
Views: 971
Reputation: 143906
You need the "/" in your regex in order for it to work. But if your links are relative, browsers will try to resolve them like http://domain.com/page/some.css
instead of http://domain.com/some.css
. You need to add this to the top of all your pages:
<base href="/">
Or whatever the base of all your relative links should be. And your rules should look like this:
RewriteRule ^post/([^/]*)$ /post.php?p=$1 [L]
RewriteRule ^user/([^/]*)$ /user.php?p=$1 [L]
And this should rewrite urls like http://www.domain.com/post/15
and http://www.domain.com/user/USERNAME
Upvotes: 1
Reputation: 9748
Try looking into some PHP Frameworks. They'll help you do this as well as helping you keep your code cleaner and more manageable
Upvotes: 0