的.htaccess定期EX pression问题 [英] .htaccess regular expression issue

查看:155
本文介绍了的.htaccess定期EX pression问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有这方面的答案,所以我在这里张贴。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆