3

I am using mod_rewrite for search engine friendly URLs. I have this line that works great:

RewriteRule ^Pay ./pay.php [L] #Pay

but I would like to also have a match if a visitor types http://example.com/pay (note the lowercase). I have tried using NC but I get a 500 error. I have tried making 2 separate rules one uppercase and one lowercase, but again 500 error.

Can this be done on one line using a regular expression? Something like:

RewriteRule ^([P-p])ay ./pay.php [L] #Pay

If so what is the proper way? If not how could I accomplish this without using mod_spelling?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Sabyre
  • 43
  • 1
  • 5

2 Answers2

1
RewriteRule ^Pay ./pay.php [L] #Pay

You get a 500 error because of an endless rewrite loop. If you simply make the above case-insensitive then the rewritten URL ie. /pay.php matches the now case-insensitive pattern ^Pay etc. etc.

In this case, you can simply make your pattern more restrictive and match just "pay", not any URL-path that simply starts "pay". For example:

# Pay
RewriteRule ^pay$ pay.php [NC,L]

I've also removed the ./ prefix on the susbstitution, this is not required. Also, Apache does not support line-end comments.

Be aware, however, that in making this rewrite case-insensitive it potentially results in duplicate content.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • Thank you MrWhite for that! What did adding the '$' change? Also, what do you mean by duplicate content? Example? – Sabyre Sep 01 '17 at 15:39
  • Ok, from this I earned case insensitive rewrites. Waaay better than my attempt! – TomTomTom Sep 01 '17 at 15:42
  • $ marks the end of the rewritten url - means it doesn't match, if there is something behind "pay", just as ^marks the beginning - it doesn't match for example ipay. – TomTomTom Sep 01 '17 at 15:44
  • Thank you @TomTomTom, that makes sense. I guess I am trying to understand why `RewriteRule ^Pay ./pay.php [NC,L]`generates an endless loop vs `RewriteRule ^pay$ pay.php [NC,L]` – Sabyre Sep 01 '17 at 15:49
0

I don't know about a simple case-insensible rewrite rule, but your attempt to match P or p is very near to correct. Just omit the dash and the brackets- it works. This is what I tested for you (as simple as possible):

RewriteRule ^/[Pp]ay /pay.php

Characters in square brackets are just alternatives for 1 character, so

RewriteRule ^[Pp][Aa][yY] /pay.php

would match any combination of lower case and upper case. I'm not sure, though, why your version didn't work, because it means "match any character between P and p", which should be P,Q,R,...Z,some-punktuation,a,b,c...p. I haven't tested your version.

The brackets () shouldn't do any harm, they just mean "extract whatever you find there and make it accessable as '$1' in replacement string".

Suggestion: try with static files. Create test.html with some dummy content ("this is test.html", for example), try to access it with your browser via the web server you are testing. Then add a rule

rewriterule ^/best.html$ /test.html

and access best.html - you should see test.html. Keep changing small bits of your rewrite rule until you find the piece that went wrong.

HBruijn
  • 77,029
  • 24
  • 135
  • 201
TomTomTom
  • 611
  • 3
  • 6