How to select only numbers in URLs using .htaccess?

22 January 2023 765 Reading time: 28 second

You can use .htaccess file to select only numbers in URLs. The following example specifies that only numbers are valid in the URL:

RewriteEngine On
RewriteRule ^([0-9]+)$ index.php?id=$1 [NC,L]

In this example, the RewriteRule command is used to specify that only numbers are valid in place of any path in the URL. This path is then redirected to the index.php file and the numbers are used as the value for the id variable. The [NC, L] flags ensure that the match is case-insensitive and there is only one match.

Similar articles