【问题标题】:.htaccess rule skipping $2 not matched uri segment from the url.htaccess 规则从 url 跳过 $2 不匹配的 uri 段
【发布时间】:2021-09-03 00:51:51
【问题描述】:

我想要下面的请求和响应

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 进行一些更改,但到目前为止没有任何帮助,之前有人这样做过吗? 如果需要更多描述,请告诉我

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite friendly-url


    【解决方案1】:

    使用您显示的示例/尝试,请尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

    将这些规则保留在 .htaccess 规则文件的顶部。我假设您有规则可以在您的 htaccess 规则文件中提供新重写的 url。

    RewriteEngine ON
    ##1st kind of urls, to remove / from end of it.
    RewriteRule ^([a-zA-Z]{2})/?$ $1 [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})/?$ $1 [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}/)*(.*)/?$ $1/$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)/?$ $1 [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)/?$ $1 [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)/)*(.*)/?$ $1/$2 [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]
    

    【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多