重写规则语言和页面参数



我尝试了这个网站提供的所有解决方案,但都不起作用。

我是这样做的:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /folder/
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)$ index.php?page=$1 [L]
RewriteRule ^([a-z]{2})/([^/]*)$ index.php?lang=$1&page=$2 [L]

实际上,我想以这种格式访问页面:

website.com/en
website.com/en/download
website.com/download

并翻译成:

website.com/index.php?lang=en
website.com/index.php?lang=en&page=download
website.com/index.php?page=download

解决方案吗?

谢谢。

您的第一条和最后一条规则都很好,但是对于最后一个选项—一个没有语言文件夹的页面—您应该简单地将其重定向到index.php,并确保没有其他重复,如下所示

RewriteRule ^([a-z]{2})/(.*)$ index.php?lang=$1&page=$2 [L]
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]

我刚刚在我的开发服务器上测试了以下规则,它们似乎达到了你想要的效果。

RewriteBase /folder/
RewriteRule ^([a-zA-Z]{2})$ index.php?lang=$1 [NC,L]
RewriteRule ^([a-zA-Z]+)$ index.php?page=$1 [NC,L]
RewriteRule ^([a-z]{2})/([a-zA-Z]+)$ index.php?lang=$1&page=$2 [NC,L]

在测试脚本/folder/index.php中:

<?php
    print_r($_GET);
?>

示例url:

https://localhost/en/
    ->  Array ( [lang] => en )
https://localhost/download/
    ->  Array ( [page] => download ) 
https://localhost/en/download/
    ->  Array ( [lang] => en [page] => download ) 

最新更新