htaccess-based redirection of numeric-only URLs
-
18-06-2021 - |
Question
Let me explain by example — I want the web server to redirect a URL like http://example.com/-NUM
to http://example.com/?p=NUM
i.e. for instance, http://example.com/-121
should be redirected to http://example.com/?p=121
.
In this case, what should the .htaccess redirection rules look like?
Solution
The pattern ^-(\d+)$
will match numeric URI's beginning with a hyphen and capture into $1
.
RewriteEngine On
RewriteRule ^-(\d+)$ /?p=$1 [L]
If this is a PHP script, for example, you may need to use index.php
as the target:
RewriteRule ^-(\d+)$ index.php?p=$1 [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow