简体   繁体   中英

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

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]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM