.htaccess 使用htaccess将显示语言转换为子文件夹

.htaccess 使用htaccess将显示语言转换为子文件夹,.htaccess,.htaccess,对于我在本地拥有的网站,我需要使用htaccess文件将显示语言设置为子文件夹。 例如,如果我选择用德语显示站点,而不是像这样显示它=>local\u ip/test/index.php?lang=de,我希望它像这样显示=>local\u ip/test/de/。 另外,如果我在没有设置语言的情况下键入url,我希望htaccess自动重定向到德语,从so=>local\u ip/test/tour/mountain/到so=>local\u ip/test/de/tour/mountain

对于我在本地拥有的网站,我需要使用htaccess文件将显示语言设置为子文件夹。 例如,如果我选择用德语显示站点,而不是像这样显示它=>
local\u ip/test/index.php?lang=de
,我希望它像这样显示=>
local\u ip/test/de/
。 另外,如果我在没有设置语言的情况下键入url,我希望htaccess自动重定向到德语,从so=>
local\u ip/test/tour/mountain/
到so=>
local\u ip/test/de/tour/mountain

我当前的htaccess文件结构如下:

DirectoryIndex index.php 
RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
已解决

在几次尝试之后,在阅读了这个社区中发布的几个支持请求之后,我找到了一种方法,使系统能够识别所有其他规则。我所要做的就是把它们放在支持我的用户提供的最后一条规则之前。这是最后的代码

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]

您可以使用以下选项:

RewriteEngine on

#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]

您当前的htaccess位于哪个文件夹中?在test=>local\u ip/test/中。如果未指定语言,如何自动添加它?例如local_ip/test/tour/mountain-local_ip/test/de/tour/mountain中的local_ip/test/tour/mountain,使用您推荐的代码,如果我尝试键入local_ip/test/de/url,url将转换为local_ip/test/de/index.php?lang=de@webdevfp请立即尝试更新的版本。现在仅当我键入local_ip/test时,它才起作用。它正确地将url转换为本地_ip/test/de/。但是,如果我键入local_ip/test/about.php,它不会将其转换为local_ip/test/de/about.php。太棒了!还有两个请求:1)当我将网站在线移动到test.com域时,我将如何更新.htaccess规则?2) 如果我想增加意大利语和英语,规则应该如何更新?很好。非常感谢你。我将提出进一步问题的新请求。