I want to redirect some pages of my site.
These pages have the following form: xxxx.html (x=digit, up to 4 digits), eg 1.html, 24.html, 6875.html etc I want to redirect these pages to the following format: index.php?id=xxxx.
The solution I have already apply is the following line in the htaccess file:
RewriteRule ([0-9]+)\.html$ /index.php?id=$1 [R=302,QSA,L]
The redirection works perfectly but the problem is that I want to limit this rule for pages that have up to 4 digits, eg 6789.html, 234.html. For example I don't want the rule to be applied for the page 55555.html.
This is probably what you are looking for:
RewriteEngine on
RewriteRule ^/?([0-9]{1,4})\.html$ /index.php?id=$1 [R=302,QSA,L]
That slightly modified pattern will work in the http servers host configuration and likewise in dynamic configuration files (.htaccess
style files).
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).