简体   繁体   English

是否为PHP的“ lang?=”使用了mod_rewrite超过一页?

[英]mod_rewrite for PHP's “lang?=” for more than one page?

I currently use the follwoing code in my .htaccess file 我目前在.htaccess文件中使用以下代码

RewriteEngine On
RewriteBase /

# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array): 使用该代码,例如,每次我在URL末尾键入/en时,它会将我重定向到/?lang=en (从PHP数组加载英语内容):

For instance: 例如:

example/en redirects me to example/?lang=en while keeping example/en in the URL. example/en将我重定向到example/?lang=en同时将example / en保留在URL中。

But I also have a thanks.php page and the code above clearly just work for the index.php page. 但是我也有一个thank.php页面,上面的代码显然只适用于index.php页面。

How can I make the rewrite rule to work for both index.php and thanks.php page? 如何使重写规则对index.phpthanks.php页面均有效?

The most straight-forward way is just to do this: 最简单的方法就是这样做:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]

If you want to make it more general, you have the option of white-listing files, like this: 如果要使其更通用,可以选择将文件列入白名单,如下所示:

RewriteCond $1    ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]

...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page: ...其中(thanks)是您希望使用此功能的文件的管道分隔列表,或者您可以接受每个请求作为对现有PHP页面的传递:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM