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

Member

by elliott , in category: SEO , 2 years ago

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

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

by rusty.rosenbaum , 2 years 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.

by harrison.goodwin , a year ago

@elliott 

Note: Make sure to backup your .htaccess file before making any changes.


To remove the id variable from the URL using .htaccess, you can follow the steps below:

  1. Enable mod_rewrite by adding the following line to the top of your .htaccess file if it's not already there:
1
RewriteEngine On


  1. Add the following rule to remove the id variable from the URL:
1
2
3
4
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^id=(.*)
RewriteRule ^(.*)$ /$1? [R=301,L]


This rule checks if the query string contains the id parameter and redirects to the same URL without the id parameter. The R=301 flag ensures that a permanent redirect (301) is issued, and the L flag stops further rule processing.

  1. Save the changes to your .htaccess file.


After following these steps, any URL with the id parameter will be redirected to the same URL without the id parameter. For example, example.com/page.php?id=123 will be redirected to example.com/page.php.


Please note that this rule assumes the id parameter is always the first parameter in the query string. If it's not the case, you may need to modify the regular expression in the RewriteCond line accordingly.


Additionally, if you don't want the URL to be redirected and instead want to internally rewrite it, you can change the R=301 flag to L. For example:

1
RewriteRule ^(.*)$ /$1 [L]


This will internally rewrite the URL without changing the visible URL in the browser.

by harrison.goodwin , a year ago

@elliott 

Just to add on to the previous responses, if you want to also hide the .php extension from the URL, you can include an additional rule in your .htaccess file. Here is an example:


1 2 3 4 5 6


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


This rule will internally rewrite a URL in the format /filename/id/ to /filename.php?id=id. This way, you can access the page without explicitly showing the .php extension in the URL.


For example, you can access example.com/page/123 instead of example.com/page.php?id=123.


Make sure to save the changes to your .htaccess file and test the URLs to ensure they are working as expected.