0

I try to rewrite this URL. Group 4 is optional and looks until question mark. Group 5 must be captured even without g4 or slashes. Group 4 must be captured without Group 3.

https://www.example.de/prefix/group1/group2/group3-suffix/group4(optional)?group5

The Regex seems to work exactly as needed in most regex flavors, but in my .htaccess, group 5 is always empty.

My rewrite looks like this:

RewriteRule ^prefix/([^/]*)/([^/]*)/([^/]*)-suffix?/?((?:(?![?/]).)*)/?\??(.*)?/?$ /target.jsp?g1=$1&g2=$2&g3=$3&g4=$4&$5 [L]

Thank you for any help!

wp78de
  • 18,207
  • 7
  • 43
  • 71
metamagikum
  • 1,307
  • 15
  • 19
  • 1
    Because `?group5` is a query string?! – revo Dec 02 '17 at 18:15
  • G5 would be everything after the questionmark. Indeed this can be a query string. G4 looks until the qm or a forward slash. – metamagikum Dec 02 '17 at 18:41
  • `RewriteRule` doesn't look at query strings, hence results differ. – revo Dec 02 '17 at 18:46
  • I wish to had never asked this question. You brought me to the right path. I need to use RewriteCond to get the query params. – metamagikum Dec 02 '17 at 18:55
  • Possible duplicate of [Match Question Mark in mod\_rewrite rule regex](https://stackoverflow.com/questions/822421/match-question-mark-in-mod-rewrite-rule-regex) – wp78de Dec 02 '17 at 20:43

1 Answers1

1

Since RewriteRule doesn't look at query strings you need to use a predefined flag to append old query strings to target URL known as QSA

RewriteRule ^prefix/([^/]*)/([^/]*)/([^/]*)-suffix?/?([^/]*)/?$ /target.jsp?g1=$1&g2=$2&g3=$3&g4=$4 [L,QSA]
revo
  • 47,783
  • 14
  • 74
  • 117