htaccess to 301 redirect any URL that ends with 10 numbers

htaccess to 301 redirect any URL that ends with 10 numbers - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, apache, 301-redirect, web-development, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


How can I setup .htaccess to redirect any URL that ends with 10 digits to home?



For example, I want to redirect these URLs:




  • example.com/abc/1234567890 -> example.com

  • example.com/xyz/0001112223 -> example.com

  • example.com/foo/bar -> ignored


Solution :

Using mod_rewrite in .htaccess:



RewriteEngine On
RewriteRule d10$ / [R=301,L]


The RewriteRule pattern (regex):

- d is the shorthand character class for a digit 0-9.

- 10 is for 10 of the preceding pattern (ie. 10 digits).

- $ is an anchor for the end of the string - these digits must appear only at the end of the URL-path.



Whether you should perform a redirect like this is another matter. Multiple redirects to the homepage are likely to be seen as a soft-404 anyway.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect