質問

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

役に立ちましたか?

解決

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]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top