我有url www.foo.com
,它有两种语言EN
和CN
。我有about.php
、product.php
等页面
我想把URL www.foo.com/about.php?lang=cn
写成www.foo.com/cn/about
,同样的方式,www.foo.com/en/product
,等等
我使用nginx服务这个php网站。
我可以将。php文件隐藏为location / {
try_files $uri $uri/ $uri.php?$query_string;
}
我试着写这样的东西
location / {
try_files $uri $uri/$query_string/ $uri.php?$query_string;
}
但不知何故似乎不工作。
在文件夹中添加。htaccess文件。
。htaccess:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([A-Za-z0-9-]+)/about/?$ about.php?lang=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/product/?$ product.php?lang=$1 [NC,L]
我的建议如下:在这个示例中,您"松散"了所有其他查询参数。
location / {
# for every query string parameter nginx provides a variable
# $arg_<name>
if ($arg_lang != ''){
set $old_lang $arg_lang;
set $args lang='';
set $arg_lang '';
# questionmark after $uri drops all query params
rewrite ^ /$old_lang$uri? redirect; # or permanent
}
}