Moseleyi
Moseleyi

Reputation: 2879

Htaccess + regex - server returns 500 Internal Server error

I have following regex:

^(?:([a-z]{2})\/)?.*?(?:\/?([\w\-]+))$

which basically means that I want to capture the two-letter language code and the last string after the slash.

It uses an answer from different question. I could not return matches in a reversed order so settled for the last one.

Unfortunately my server returns 500 Internal Server Error. I tested it and it looks that it doesn't like the star * in the middle of the regex.

It also doesn't like .+ if I insert it instead of .*

Below example URLs:

page
page/page1
page/page2/page3

en
en/page
en/page/page2
en/page/page2/page3

I would like the matches to be:

page
page1
page3

en
en & page
en & page2
en & page3

I do not understand why * or + would make server crash. I couldn't find any details, so hoping somebody can shed some light.

Link to Regex101: https://regex101.com/r/hW9eZ4/10

Upvotes: 1

Views: 230

Answers (1)

anubhava
anubhava

Reputation: 785581

You may use rule with a regex that uses branch reset group feature of PCRE:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?|([a-z]{2})|(?:([a-z]{2})/)?.*?([\w-]+))/?$ index.php?a=$1&b=$2 [L,QSA]

RegEx Demo

I have tested in my Apache and it works pretty well. Make sure to comment all other rules while testing this.

Upvotes: 1

Related Questions