Home > Net >  Rewrite parameter url to normal url but it should get original url parameters after redirect
Rewrite parameter url to normal url but it should get original url parameters after redirect

Time:12-27

Original URL: https://www.example.com/all_users/user.php?id=1&subtype=p

Expected URL: https://www.example.com/user/john-doe.html

I tried using following rule, it redirects to expected URL but I can't use parameters (id and subtype) with new URL.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id\=1&subtype\=p$
RewriteRule ^all_users/user\.php$ https://www.example.com/user/john-doe.html? [R=301,L]

How can I use parameter values with new URL.

Example:

After redirect, new URL will be https://www.example.com/user/john-doe.html Then I should able to get original parameters value with new URL.

$id = $_GET['id'];
$type = $_GET['subtype'];

CodePudding user response:

With your shown samples and attempts, please try following htaccess rules.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##External redirect here.
RewriteCond %{THE_REQUEST} \s/all_users/user\.php\?id=\d &subtype=p\s [NC]
RewriteRule ^  user/john-doe.html? [R=301,L]
##Internal rewrite from here..
RewriteRule ^user/john-doe\.html/?$  all_users/user.php?id=1&subtype=p [QSA,NC,L]
  • Related