How can I remove the id variable from the URL with htaccess?

Member

by elliott , in category: SEO , 10 months ago

How can I remove the id variable from the URL with htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by rusty.rosenbaum , 10 months ago

@elliott 

To remove the id variable from the URL with .htaccess, you can use mod_rewrite to write a rule that performs a URL rewrite. Here is an example of such a rule:

1
2
3
4
5
6
7
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^s?]+).php?id=([^s&]+)s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?id=$2 [L,QSA]


This rule first captures the id variable from the query string and rewrites the URL to be in the format /filename/id/. The second set of rules maps the rewritten URL back to the original URL with the query string.