Search code examples
regex.htaccesshttp-redirectpcreinternal-server-error

Regex on htaccess file gives INTERNAL REDIRECT error


I'm trying to create a redirect rule that matches the following urls:

en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/9/
en/property/rotterdam-centrum/appartment/lorem-ipsum-dolor-sit-amet/9
en/property/lorem-ipsum-dolor-sit-amet-9/
en/property/lorem-ipsum-dolor-sit-amet/v9/
en/property/lorem-ipsum-dolor-sit-amet/v-9/
en/property/lorem-ipsum-dolor-sit-amet/v-9
en/property/rotterdam/apartment/branch/lorem-ipsum-dolor-sit-amet-9/
en/property/rotterdam/lorem-ipsum-dolor-sit-amet-9/
en/property/rotterdam/lorem-ipsum-dolor-sit-amet-9434343/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-243249/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v2342349/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/243249/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/?test=1
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/test/one/?success=3
en/property/rotterdam/apartment/2-rooms/lorem-ipsum-dolor-sit-amet/v-9/blah/1234/blahh/55555/?sass=123
en/property/rotterdam/appartment/lorem-ipsum-dolor-sit-amet/9234234
en/property/rotterdam/lorem-ipsum-dolor-sit-amet/9234234
en/property/lorem-ipsum-dolor-sit-amet/9234234
en/property/9234234
en/property/rotterdam/appartment/lorem-ipsum-dolor-sit-amet/9/test/132?algo=123

All urls that need to be match can be seen here
https://regex101.com/r/4TPEhy/1

I'm facing two issues, first one is that two urls are not matching:

en/property/rotterdam/apartment/2-rooms/lorem-ipsum-dolor-sit-amet/v-9/blah/1234/blahh/55555/?sass=123
en/property/9234234

And the second one, something on that regex is causing an interal redirect error.

This is my regex (the best case that I got so far)

^(en\/)?property\/[A-Яa-я0-9\-_\/]+?([\d]+)(.*)$

And this is my rewrite rule

RewriteRule ^(en\/)?property\/[A-Яa-я0-9\-_\/]+?([\d]+)(.*)$ $1properties/property/$2$3&%{QUERY_STRING} [NC,L]

The regex seems to be working fine on the tool (regex101), but as soon as a try to refresh the page applying that rule I get a 500 - Internal Server Error.

I need to match all the cases, I've tried different combinations for hours, but I'm missing something that's causing those issues.

On the .htaccess the groups that I need are $2 and $3, since $2 contains the property ID which I'm using on my script and $3 is all the rest of the URL like any other extra parameter or the query string.

The reason for so many different configurations is that users can set their own URL format, but limited to the options listed above.

This is an extract from the apache logs when this URL is requested

https://localhost/en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/test/132?blah=123

Logs

php74         | [Mon May 31 12:19:12.375537 2021] [rewrite:trace3] [pid 64] mod_rewrite.c(483): [client 172.18.0.1:63664] 172.18.0.1 - - [localhost/sid#7fd3d4ab1958][rid#7fd3d29d73c0/initial/redir#10] [perdir /var/www/html/] add per-dir prefix: en/properties/property/9/test/132&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123 -> /var/www/html/en/properties/property/9/test/132&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123

Solution

  • You may try this rewrite rule:

    RewriteEngine On
    
    RewriteRule ^(en/)?property/(?:\S*?[-/v])?(\d+)(/.*)?$ $1properties/property/$2$3 [NC,L]
    

    Updated RegEx Demo

    We are capturing property ID after a - or / or v characters.