How to remove numbers from url in .htaccess?

Member

by stephon , in category: SEO , 6 months ago

How to remove numbers from url in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by jacey.lubowitz , 6 months ago

@stephon 

To remove numbers from a URL using .htaccess, you can use regular expressions and the RewriteRule directive. Here's an example of how you can achieve this:

  1. First, make sure that the Apache mod_rewrite module is enabled. You can do this by checking if the module is listed in the "Loaded Modules" section of your Apache configuration, or by running the following command in a terminal:
1
2
sudo a2enmod rewrite
sudo service apache2 restart


  1. Create or open the .htaccess file in the root directory of your website.
  2. Add the following lines to your .htaccess file:
1
2
RewriteEngine On
RewriteRule ^([a-zA-Z]+)/[0-9]+/(.*)$ /$1/$2 [R=301,L]


This rule will remove numbers from the second segment of the URL. For example, it will redirect www.example.com/category/123/product to www.example.com/category/product.

  1. Save the .htaccess file and test your website to see if the numbers have been removed from the URLs.


Note: The [R=301,L] flags in the RewriteRule indicate that it should be a permanent redirect (301) and that no further rewrite rules should be processed (L).


Also, make sure to customize the regular expression (([a-zA-Z]+)) to match the specific URL structure of your website if necessary.