Home > database >  Apache vhost : redirect repeating pattern does not work using mod-rewrite
Apache vhost : redirect repeating pattern does not work using mod-rewrite

Time:07-16

I need to prevent a repetition that may occur on my website with Apache ModSec : I want to redirect this kind of link :

www.example.com/forbidden/forbidden/forbidden/forbidden/index.html?uid=Ys7RZ2RWjrRtTYDoG5cEQgAAAAI

... to this :

www.example.com/forbidden/index.html?uid=Ys7RZ2RWjrRtTYDoG5cEQgAAAAI

note : the "forbidden" part can have many more repetitions

Using RewriteMatch I wrote this logic :

RewriteEngine   On
RewriteRule     ^/forbidden(.*)index.html\?uid=(.*)$    /forbidden/index.html?uid=$2 [R,L]

But it does not work : I still get a 404 and i'm not rederected to the www.example.com/forbidden/index.html?uid=Ys7RZ2RWjrRtTYDoG5cEQgAAAAI endpoint.

Is the rewrite rule syntax valid for my use case ?

CodePudding user response:

You can use this link to redirect multiple repetitions of /forbidden:

RewriteRule ^/?(forbidden/){2,}(index\.html)$ /$1$2 [L,NC,NE,R=301]

Note that you don't match query string in RewriteRule but in this case you don't even need to match specific query string since query string is automatically appended to the redirected URL.

  • Related