thatgerhard
thatgerhard

Reputation: 303

How do I create dynamic friendly urls for a query string?

So currently I have URLs that looks like this:

http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard

I want the above to look like this:

http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)

I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.

This is what I have atm:

RewriteBase /
RewriteEngine On

RewriteRule ^((.)*)$ /?v=$1&p=$

I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.

Any help or direction will be greatly appreciated. :)

Upvotes: 0

Views: 72

Answers (1)

Ben
Ben

Reputation: 5139

You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.

RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]

Upvotes: 1

Related Questions