user3304303
user3304303

Reputation: 1043

How do I redirect URLS with IDs using .htaccess

I need to redirect traffic from:

example.com/profilepage.php?profileid=123456

to

example.com/profile-page.php?profileid=123456  

Note: only difference is the new hyphen

I tried several variations with no luck, including

RewriteRule ^/?profilepage.php?profileid=([^/]+)/?$ /profile-page.php?profileid=$1 [L,QSA]

RewriteRule ^/?profilepage.php?profileid=([^/]+)/$ /profile-page.php?profileid=$1 [L,QSA]

RewriteRule ^/?profilepage.php?profileid=([^/]+) /profile-page.php?profileid=$1 [L,QSA]

As you can see, I'm unsure what to do AFTER the profileid regex stuff

Upvotes: 0

Views: 83

Answers (2)

Croises
Croises

Reputation: 18671

No need to test the query string, by default it remains unchanged.
You can use:

RewriteEngine on
RewriteRule ^profilepage\.php$ profile-page.php [NC,L,R=301]

Upvotes: 1

Sahil Gulati
Sahil Gulati

Reputation: 15141

Try something like this. You have to explicitly match QUERY_STRING

Problems:

1. You should match query string with %{QUERY_STRING}

2. Change your regular expression from this ([^/]+) to (\d+)

Note:

Make sure your matched ID is in %1 instead of $1

You can check this .htaccess here

RewriteEngine on

RewriteCond %{QUERY_STRING} ^profileid=(\d+)$
RewriteRule ^profilepage\.php$ /profile-page.php?profileid=%1 [L,R]

Upvotes: 1

Related Questions