Using htaccess, how do you re-write for parameters over 8 characters or more only? Using htaccess, how do you re-write for parameters over 8 characters or more only? unix unix

Using htaccess, how do you re-write for parameters over 8 characters or more only?


How about:

RewriteEngine OnRewriteCond %{REQUEST_URI} !^/script.php.*$RewriteRule ^([^\/]{8,})[^\/]*$ "/script.php?id=$1" [R,L]
  • Uses RewriteCond as extra guard against a redirect loop

By default, the original query string is discarded when adding a new one, so if you want to preserve the original (so, example.com/12345678?utm_campaign=foo goes to example.com/script.php?id=12345678&utm_campaign=foo) use the [QSA] flag, like this:

RewriteEngine OnRewriteCond %{REQUEST_URI} !^/script.php.*$RewriteRule ^([^\/]{8,})[^\/]*$ "/script.php?id=$1" [QSA,R,L]

If the ID is always just numbers, you could change the Regex to ^(\d{8,})[^\/]*$ to make it a little more foolproof.