0

I'm trying to build a website that may be called from the URL bar with any one of the following examples:

domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html

These page requests need to hit my .htaccess template and ultimately be converted into this php call:

/index.php?lng=???&tpl=???

I've been trying to make RewriteCond and RewriteRule code that will safely deal with the dynamic nature of the URLs I'm trying to take in but totally defeated. I've read close to 50 different websites and been working on this for almost a week now but I have no idea what I'm doing. I don't even know if I should be using a RewriteCond. Here is my last attempt at making a RewriteRule myself:

RewriteRule ^(([a-z]{2})(-[a-z]{2})?)([a-z0-9-\./]*) /index.php?lng=$1&tpl=$4 [QSA,L,NC]

Thanks for any help,

Vince

Jack Bonneman
  • 1,821
  • 18
  • 24
Vince
  • 910
  • 2
  • 13
  • 29
  • FYI: When I test this on http://htaccess.madewithlove.be/ everything works no problem, but then I test on my own server I keep getting '500 Internal Server Error'. My domain wont even come up with no additional parameters unless I delete my .htaccess off the server. – Vince May 03 '13 at 18:11
  • I added a loop stopper to my htaccess and it appears to have fixed my problem but I still don't understand whats looping. RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] – Vince May 03 '13 at 19:29

1 Answers1

1

What's causing your loop is that your regex pattern matching /index.php. Why? Let's take a look:

First, the prefix is stripped because these are rules in an htaccess file, so the URI after the first rewrite is: index.php (query string is separate)

The beginning of your regex: ^(([a-z]{2})(-[a-z]{2})?), matches in in the URI

The next bit of your regex: ([a-z0-9-\./]*) matches dex.php. Thus the rule matches and gets applied again, and will continue to get applied until you've reached the internal recursion limit.

Your URL structure:

domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html

Either has a / after the country code or nothing at all, so you need to account for that:

# here -------------------v   
^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$
#      and an ending match here ------------^

You shouldn't need to change anything else:

RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • ty ty Jon, you saved my sanity. Everything else from here boils down to simple PHP string parsing which I don't have any problems with. – Vince May 03 '13 at 20:05