.htaccess 规则从 url 中跳过 $2 不匹配的 uri 段
.htaccess rule skipping $2 not matched uri segment from the url
我想要以下请求和响应
-
2
要求:https://example.com/de/fr/path
预期:https://example.com/de/path
3 #默认英文
https://example.com/def/frq/path
https://example.com/en
RewriteEngine On
#Check for files that do not contain the language string
RewriteCond %{REQUEST_URI} !^/[a-z]{2}/.*
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en [R=301,L]
#Capture the language string and present it as the variable
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/
RewriteRule ^([a-z]{2})/(.*)$ https://%{HTTP_HOST}/%1/%2 [R=301,L]
尝试对 %2 段 uri 进行一些更改,但到目前为止没有任何帮助,有人早些时候做过吗?
如果需要更多描述,请告诉我
根据您显示的 samples/attempts,请尝试以下。请确保在测试您的 URL 之前清除您的浏览器缓存。
将这些规则放在 .htaccess 规则文件的顶部。我假设您有规则来为您的 htaccess 规则文件中新重写的 url 提供服务。
RewriteEngine ON
##1st kind of urls, to remove / from end of it.
RewriteRule ^([a-zA-Z]{2})/?$ [R=301,L]
##Handle https://example.com/de/fr/en/de/en TO https://example.com/de urls.
RewriteRule ^([a-zA-Z]{2})/(?:[a-zA-Z]{2}/)*(?:[a-zA-Z]{2})/?$ [R=301,L]
##Handle https://example.com/de/fr/path TO https://example.com/de/path urls.
RewriteRule ^([a-zA-Z]{2})/(?:[a-zA-Z]{2}/)*(.*)/?$ / [R=301,L]
##handle https://example.com/def/frq/path TO https://example.com/en urls here.
RewriteRule ^([a-zA-Z]{3,})/.*$ en [R=301,L]
Online demo for 3rd rule regex
Online demo for 2nd rule regex
编辑: 或者检查以 2 个字母的特定语言开头的规则,然后尝试遵循,我使用了 en,us, de,fr 语言,您也可以根据需要添加更多语言。
RewriteEngine ON
##1st kind of urls, to remove / from end of it.
RewriteRule ^(en|us|de|fr)/?$ [NC,R=301,L]
##Handle https://example.com/de/fr/en/de/en TO https://example.com/de urls.
RewriteRule ^(en|us|de|fr)/(?:(?:en|us|de|fr)/)*(?:en|us|de|fr)/?$ [NC,R=301,L]
##Handle https://example.com/de/fr/path TO https://example.com/de/path urls.
RewriteRule ^(en|us|de|fr)/(?:(?:en|us|de|fr)/)*(.*)/?$ / [NC,R=301,L]
##handle https://example.com/def/frq/path TO https://example.com/en urls here.
RewriteRule ^([a-zA-Z]{3,})/.*$ en [R=301,L]
我想要以下请求和响应
2
要求:https://example.com/de/fr/path
预期:https://example.com/de/path3 #默认英文
https://example.com/def/frq/path
https://example.com/en
RewriteEngine On
#Check for files that do not contain the language string
RewriteCond %{REQUEST_URI} !^/[a-z]{2}/.*
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en [R=301,L]
#Capture the language string and present it as the variable
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/
RewriteRule ^([a-z]{2})/(.*)$ https://%{HTTP_HOST}/%1/%2 [R=301,L]
尝试对 %2 段 uri 进行一些更改,但到目前为止没有任何帮助,有人早些时候做过吗? 如果需要更多描述,请告诉我
根据您显示的 samples/attempts,请尝试以下。请确保在测试您的 URL 之前清除您的浏览器缓存。
将这些规则放在 .htaccess 规则文件的顶部。我假设您有规则来为您的 htaccess 规则文件中新重写的 url 提供服务。
RewriteEngine ON
##1st kind of urls, to remove / from end of it.
RewriteRule ^([a-zA-Z]{2})/?$ [R=301,L]
##Handle https://example.com/de/fr/en/de/en TO https://example.com/de urls.
RewriteRule ^([a-zA-Z]{2})/(?:[a-zA-Z]{2}/)*(?:[a-zA-Z]{2})/?$ [R=301,L]
##Handle https://example.com/de/fr/path TO https://example.com/de/path urls.
RewriteRule ^([a-zA-Z]{2})/(?:[a-zA-Z]{2}/)*(.*)/?$ / [R=301,L]
##handle https://example.com/def/frq/path TO https://example.com/en urls here.
RewriteRule ^([a-zA-Z]{3,})/.*$ en [R=301,L]
Online demo for 3rd rule regex
Online demo for 2nd rule regex
编辑: 或者检查以 2 个字母的特定语言开头的规则,然后尝试遵循,我使用了 en,us, de,fr 语言,您也可以根据需要添加更多语言。
RewriteEngine ON
##1st kind of urls, to remove / from end of it.
RewriteRule ^(en|us|de|fr)/?$ [NC,R=301,L]
##Handle https://example.com/de/fr/en/de/en TO https://example.com/de urls.
RewriteRule ^(en|us|de|fr)/(?:(?:en|us|de|fr)/)*(?:en|us|de|fr)/?$ [NC,R=301,L]
##Handle https://example.com/de/fr/path TO https://example.com/de/path urls.
RewriteRule ^(en|us|de|fr)/(?:(?:en|us|de|fr)/)*(.*)/?$ / [NC,R=301,L]
##handle https://example.com/def/frq/path TO https://example.com/en urls here.
RewriteRule ^([a-zA-Z]{3,})/.*$ en [R=301,L]