remove query string from url using .htaccess

How to Remove URL Parameters using .htaccess

Sometimes you may need to remove end part of URL or remove query string from URL. Here is how to remove URL parameters using .htaccess in Apache web server. This is useful when you are consolidating multiple URLs into single URL or you don’t need the URLs with paramters to be indexed by search engines.


How to Remove URL Parameters using .htaccess

Here are the steps to remove query string from URL.


1. Open .htaccess file

Open terminal and run the following command to open .htaccess file.

$ sudo vi /var/www/html/.htaccess

Also read : How to Export to CSV in NodeJS


2. Remove Query String from URL

Let us say you have the URL http://example.com/folder/category=”abc” and you want to remove category=”abc” then add the following lines

RewriteCond %{QUERY_STRING}    "category=" [NC] 
RewriteRule (.*)  /$1? [R=301,L]

Basically, we check if the queyr string contains “category=” and use RewriteRule to rewrite the URL without the query string. Replace “category” with your query string parameter.

If you use WordPress, then add the above lines before the following

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

but after

RewriteBase /

Also read : How to Make Post Request with cURL


3. Restart Apache server

Restart Apache server to apply changes

$ sudo service apache2 restart

Open browser and enter http://example.com/folder/category=abc and it will redirect to http://example.com/folder

Also read : How to Set Apache PATH Environment Variable


Leave a Reply

Your email address will not be published. Required fields are marked *