.htaccess重定向+重写:在保留初始uri的同时,使用语言代码对uri进行前缀 [英] .htaccess redirect + rewrite: Prefix uri with language code while keeping initial uri

查看:99
本文介绍了.htaccess重定向+重写:在保留初始uri的同时,使用语言代码对uri进行前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写/重定向传入的请求,以便它们始终以语言代码为前缀,但希望保留最初请求的路径:

I am trying to rewrite/redirect incoming requests so that they will always be prefixed by a language code but would like to keep the originally requested path:

example.org/de/     -> rewrite to example.org?lang=de
example.org         -> redirect to example.org/de/ which will then be rewritten
                       to example.org?lang=de
example.org/en/     -> rewrite to example.org?lang=en
example.org/foo/bar -> redirect to example.org/de/foo/bar wich will 
                       then be rewritten to example.org/foo/bar?lang=de

这些是我对此的想法:

RewriteBase /

# No 'lang' key in query string 
RewriteCond %{QUERY_STRING} !lang=(en|de) 

# AND request_uri does not start with 'en' or 'de'
RewriteCond %{REQUEST_URI} !^(en|de) 

# Redirect to lang prefixed uri while keeping the initial uri
RewriteRule ^(.*)$ de/%{REQUEST_URI} [L,R=301,QSA]

# Add language prefix as query parameter
RewriteRule ^(en|de) ?lang=$1 [L,QSA]

此外,我想保留传入请求的所有查询字符串,并将其附加到重定向/重写的uri中. 实际上,我的想法对我来说听起来很合理,但是我得到了无尽的重定向.如果您能指出正确的方向,我会很高兴,因为我从几个小时以来迷失了.

Furthermore I would like to keep any query strings of the incoming request and append it to the redirected/rewritten uri. Actually my thoughts sound logic to me but I'm getting an endless redirect. I would be too happy if you could point me in the right direction since I'm lost since hours and hours

更新

最后,在anubhava的帮助下,我可以设法解决整个问题,并希望分享最终的解决方案

Finally with the help of anubhava I could manage to solve this whole issue and would like to share the final solution

# 1) Prefix ALL request uris with /<lang>/. Depending on the `Accept-Language` 
#    header 'de' or the default 'en' is used as prefix.
# 2) Rewrite real files by stripping off the /<lang>/ prefix but still add 
#    the ?lang=<lang> query parameter
# 3) Add a trailing slash and append ?lang=<lang> query parameter


RewriteEngine On

RewriteBase /

# No 'lang' key in query string and 'de' as accepted language header.
# Redirect to 'de' prefixed uri
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule !^(en|de)(/.*)?$ de%{REQUEST_URI} [L,NC,R]

# Redirect anybody without 'de' as accepted language to the 'en' version
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
RewriteRule !^(en|de)(/.*)?$ en%{REQUEST_URI} [L,NC,R]

# Strip lang prefix in case this a real file
RewriteCond %{REQUEST_URI} ^/(en|de)(/.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}%2 -f 
RewriteRule ^(en|de)(/.*)$ $2?lang=$1 [L,NC]

# Add trailing slash to those request which make it till here
RewriteRule ^(en|de)(/.*)?/?$ $2/?lang=$1 [L,NC,QSA]

非常感谢您的帮助!

推荐答案

您可以使用如下规则:

RewriteEngine On

# No 'lang' key in query string 
RewriteCond %{QUERY_STRING} !lang=(en|de) [NC]
# Redirect to lang prefixed uri while keeping the initial uri
RewriteRule !^(en|de)/ /de/%{REQUEST_URI} [L,NC,R=301]

RewriteRule ^(en|de)(/.*)?$ $2?lang=$1 [L,NC,QSA]

清除浏览器缓存后进行测试.

Test this after clearing your browser cache.

这篇关于.htaccess重定向+重写:在保留初始uri的同时,使用语言代码对uri进行前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆