Ad

How To Redirect Old Url With Parameters To New Seo Url Using Htaccess

- 1 answer

want to redirect param urls to seo friendly urls

https://example.com/a.php?slug=vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present

to

https://example.com/vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present

and

https://example.com/tag.php?id=Virtual-reality

to

https://example.com/tag/Virtual-reality

my current .htaccess code

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)/?$ a.php?slug=$1 [L]


RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
Ad

Answer

Have it this way:

Options -MultiViews
RewriteEngine On    

RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]

# external redirect to redirect old URL to pretty URL    
RewriteCond %{THE_REQUEST} /a\.php\?slug=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

RewriteCond %{THE_REQUEST} /tag\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /tag/%1? [R=301,L,NE]

RewriteRule ^tag/([\w-]+)/?$ tag.php?id=$1 [L,QSA,NC]

# internal rewrite to forward to actual URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ a.php?slug=$1 [L,QSA]
Ad
source: stackoverflow.com
Ad