Home > Blockchain >  How to write htaccess rewrite rule for change link
How to write htaccess rewrite rule for change link

Time:11-12

i'm searching for rule for change link in htaccess.

Currently the link looks like this:

localhost.com/forum/profile.php?id=1

And this is the effect I would like to achieve:

localhost.com/forum/profile/1

I added the following rules:

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9] )\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]

The problem is that the page automatically redirects to the expected link, but a 404 error is returned

CodePudding user response:

You need one more rule to map forum/profile/123 to forum/profile\.php?id=123. Please clear your browser cache before testing your URLs.

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9] )\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]
RewriteRule ^forum/profile/([0-9] )/?$ /forum/profile.php?id=$1 [NC,QSA,L]
  • Related