0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That worked great. The links are exactly how I wanted them now. Didn't know about the " <base href='/'> " . Thank you so much for your answer.
0

Try looking into some PHP Frameworks. They'll help you do this as well as helping you keep your code cleaner and more manageable

1 Comment

Hey. I'm trying to do it with mod_rewrite, the .htaccess rewriter, but i'm a complete rookie at url rewriting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.