0

I have a regexp that seems to work fine when tested at regexp101.com but that does not give me the same result within mod_rewrite.

So, the URL I am trying to rewrite is:

/modeles-voiture/Nissan/Qashqai+2

The expected result is:

/modeles.php?brand=Nissan&model=Qashqai+2

The rewrite rule:

RewriteCond %{REQUEST_URI} ^/?modeles-voiture [NC]

RewriteRule \/([A-Z][\-A-Za-z]+)\/([\+\-A-Za-z0-9]+$) /modeles.php?brand=$1&model=$2 [L]

What I am getting out of the rewrite rule is:

/modeles-voiture/Nissan/Qashqai 2

Note the missing + sign, which throws off my script at modeles.php

Thanks for your help.

BernardA
  • 1,391
  • 19
  • 48
  • Maybe related to: http://stackoverflow.com/questions/5450190/how-to-encode-the-plus-symbol-in-url – TurboHz May 26 '14 at 17:22
  • Start by figuring out where the + sign disappear. If you go to /modeles.php?brand=Nissan&model=Qashqai+2 will `$_GET['model']` contain the + character or not? – WizKid May 26 '14 at 17:43
  • @turboHz, thanks for your reply. It could as well be related to that. I just tried to replace the + sign with %2b, making my URL /modeles-voiture/Nissan/Qashqai%2b2 but it gave me this:modeles-voiture/Nissan/Qashqai%2b2. So, the mechanics are not that. – BernardA May 26 '14 at 17:43

1 Answers1

0

I think you want the [B] flag.

Look at the answer to this question: How to encode special characters using mod_rewrite & Apache?

So then mod_rewrite changes your request '/tag/c++' to 'script.php?tag=c++'. But in a query string component in the application/x-www-form-encoded format, the escaping rules are very slightly different to those that apply in path parts. In particular, '+' is a shorthand for space (which could just as well be encoded as '%20', but this is an old behaviour we'll never be able to change now).

So PHP's form-reading code receives the 'c++' and dumps it in your _GET as C-space-space.

Community
  • 1
  • 1
S. Ahn
  • 633
  • 5
  • 8
  • Thanks for your reply. Replacing the + sign with #2O gives me the same results as replacing it with %2B, ie, returns me a URL like this modeles-voiture/Nissan/Qashqai%202, which can work fine if I just give a str_replace to get the + sign back. But that is valid for any special character I put in there. If I just replace the + sign in the original URL to say, '.', I can have it pass through the RewriteRule and then reconvert back at the destination php script. So, that's not really a solution, but a workaround. – BernardA May 26 '14 at 18:21
  • Did you try using the [B] flag? – S. Ahn May 26 '14 at 18:58
  • Not at first, but then I realized it and put it in. Did not work, though. In any case, I made it work with the workaround mentioned in my previous comment, ie, just replacing + with something else that Apache does not have an issue with ('.' in the case). – BernardA May 26 '14 at 19:35