的.htaccess定期EX pression问题 [英] .htaccess regular expression issue
问题描述
我还没有这方面的答案,所以我在这里张贴。
I still have no answer about this so I'm posting it here.
我想匹配的网址的.htaccess文件,然后把它改写根/语言/分/.../文档
为<$ C的形式$ C>根/分/.../ document.php?LANG =语言
林接近于打但似乎我的正则表达式不'抓'输入网址。在这里,它是:
Im close to hit but it seems my regexp doesn't 'catch' the input url. Here it is :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ^[a-z]{2}.*$/%{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z]{2})/(.*)$ $2.php?lang=$1
有人能指出我什么是错在这里?我在正则表达式也不阿帕奇重写专家。
Can someone point me out what is wrong here ? I'm no expert in regexp nor apache rewrites.
Tyvm
*编辑*
根
为域名即 mydomain.net
下面是几个例子:
mydomain.net/fr/contact
应该rewriten到
should be rewriten to
mydomain.net/contact.php?lang=fr
和
mydomain.net/en/articles/view
应该rewriten
should be rewriten
mydomain.net/articles/view.php?lang=en
等等...
推荐答案
我相信你正在寻找此配置:
I believe you are looking for this configuration:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*/|)(en|de|fr)/(.*)$ [NC]
RewriteCond %1%3.php -f
RewriteRule ^(.*/|)(en|de|fr)/(.*)$ $1$3.php?lang=$2 [NC,QSA,L]
说明:的
Explanation:
RewriteCond %{REQUEST_FILENAME} !-d
如果请求的URL不是一个目录检查。如果是的话,就不会有重写将被处理。的
RewriteCond %{REQUEST_FILENAME} ^(.*/|)(en|de|fr)/(.*)$ [NC]
检查是否URL包含在里面, / XX /
部分,其中 XX
是连接
,日
或 FR
。标志 [NC]
允许大写和小写本地字符,因此,例如 EN
或神父
将被接受。如果在链接没有这样的语言的部分,不重写将被处理。的
Checks if url contains /xx/
part inside, where xx
is one en
, de
or fr
. Flag [NC]
allows uppercase and lowercase charactes, so for example EN
or Fr
will be accepted. If there is no such "language" part in the url, no rewriting will be processed.
RewriteCond %1%3.php -f
检查是否%1%3.php
文件存在,其中%1
是(* / |)(。*)
和%3
是
比赛从previous的RewriteCond ^(* / |)(ZH | DE | FR)(。*)/ $
。如果这样的PHP文件不存在,不重写将被处理。的
Checks if %1%3.php
file exists, where %1
is (.*/|)
and %3
is (.*)
matches from the previous RewriteCond ^(.*/|)(en|de|fr)/(.*)$
. If such php file does not exist, no rewriting will be processed.
RewriteRule ^(.*/|)(en|de|fr)/(.*)$ $1$3.php?lang=$2 [NC,QSA,L]
在重写规则左条件将被匹配,如果它来到了这条线,因为它已经检查了的RewriteCond,所以现在将处理重写PHP文件,添加郎的一部分,但由于还有 [QSA]
标志,它也将让其他GET参数,所以郎将被添加到现有的参数。标志 [L]
说,这是应申请,去年重写规则,并没有更多的重写将这个网址进行处理。的
In the RewriteRule left condition will be matched if it came to this line, as it was already checked with RewriteCond, so now it will process rewriting to php file, adding lang part, but because there is also [QSA]
flag, it will keep also other GET parameters, so lang will be added to existing parameters. Flag [L]
says it is last rewriting rule that should apply and no more rewriting will be processed with this url.
这篇关于的.htaccess定期EX pression问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!