RewriteRule - a.php? A = 123 to / b / 123

I am trying to get Apache to redirect /a.php?a=123 to / b / 123 (where 123 can be any number from 1 to 9999) but cannot make it work.

This is what I have in htaccess:

RewriteEngine on

RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]

In this case, the transition to a.php? a = 123 leads to 404, but switching to just a.php works as expected. Did I try to run away? (RewriteRule ^ a.php \? A = ([0-9] +) / b / $ 1 [L]), but it still doesn't work.

What am I doing wrong?

+3
source share
1 answer

The query string is not part of the URI path verified in the directive RewriteRule. This can only be checked using the directive RewriteCond:

RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]

( /b/123 /a.php?a=123):

RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]
+7

Source: https://habr.com/ru/post/1703558/


All Articles