重定向到除/amp/URL 之外的默认语言 [英] Redirect to default language except for /amp/ URLs
问题描述
我正在努力解决重定向问题,但没有任何成功.
I'm struggling to solve a redirection but without any success.
我更改了网站的 URL,强制使用默认语言,之前是 site.com/help/,现在是 site.com/en/help/
.感谢 Stack Overflow 的帮助,我进行了重定向,但随后我遇到了 AMP 页面的新问题:site.com/amp/help/
现在被重定向到 site.com/en/amp/help/
而它们应该是 site.com/amp/en/help/
.
I changed the URLs of my site forcing a default language, before it was site.com/help/ and now it's site.com/en/help/
. Thanks to help from Stack Overflow I made the redirection, but then I faced a new problem with the AMP pages: site.com/amp/help/
are now redirected to site.com/en/amp/help/
while they are supposed to be site.com/amp/en/help/
.
再次感谢本网站的帮助,我将 URL 的结构更改为 site.com/en/help/amp/
(amp 总是在最后).为此,我不得不删除某些页面中的 .php 扩展名,并决定删除尾部斜杠.
Again, thanks to help on this site, I changed the structure of URLs to site.com/en/help/amp/
(amp always at the end). To achieve this, I had to delete the .php extension I had in some pages and also decided to remove the trailing slash.
我现在面临两个新问题:301 重定向到非 .php 页面和带有尾随斜杠的 URL 到非尾随斜杠不起作用.下面是我的 htaccess 代码.
I'm now facing two new issues: the 301 redirection to a non .php page and URLs with trailing slashes to a non trailing slash don't work. Below is my htaccess code.
RewriteEngine on
# amp
RewriteRule ^(.*/)?amp/(.+?)/?$ /$1$2/amp [R=301,NC,L]
## redirect to default language (fr)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} !/inc
RewriteCond %{REQUEST_URI} !/ajax/
RewriteCond %{REQUEST_URI} !/img/
RewriteRule ^(?![a-z]{2}(?:[/-]|$))(.*)$ /fr/$1 [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,NE,L]
## add trailing slash in front of directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+)$ /$1/ [L]
# remove .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteCond %{REQUEST_URI} !/ajax/
RewriteCond %{REQUEST_URI} !results.php
RewriteRule ^(.+)\.php(.*)$ /$1$2 [R=301,NC,NE,L]
## amp pages
RewriteRule ^(.*)/amp$ /$1?amp=1 [NC,QSA,L]
## folders of languages
#RewriteRule ^([a-z]{2}(?:-[a-z]{2})?)/(.*)$ /$2?lang=$1 [QSA,L]
RewriteRule ^([a-z]{2}|[a-z]{2}-[a-z]{2})$ /$2?lang=$1 [QSA,L]
RewriteRule ^([a-z]{2}|[a-z]{2}-[a-z]{2})/(.*)$ /$2?lang=$1 [QSA,L]
## hide .php extention
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteRule ^(.+?)/?$ $1.php [L]
推荐答案
就这样吧:
RewriteEngine on
# changed amp URLs
RewriteRule ^(.*/)?amp/(.+?)/?$ /$1$2/amp/ [R=301,NC,L]
## redirect to default language (en)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} !/img/
RewriteRule ^(?![a-z]{2}(?:[/-]|$))(.*)$ /en/$1 [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/amp/$ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,NE,L]
## add trailing slash in front of directories after lang rewrite
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+[^/])$ /$0/ [L]
# remove .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.+)\.php$ /$1/ [R=301,NC,NE,L]
## amp pages
RewriteRule ^(.+/)amp/?$ /$1?amp=1 [NC,QSA,L]
## folders of languages
RewriteRule ^([a-z]{2}(?:-[a-z]{2})?)/(.*)$ /$2?lang=$1 [QSA,L]
## hide .php extention
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteRule ^(.+?)/?$ $1.php [L]
解释这个尾部斜杠规则:
## add trailing slash in front of directories after lang rewrite
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+[^/])$ /$0/ [L]
以 URI 为例:/fr/cart
.
在后面的规则中,我们从 URL 中删除 lang
组件并将其作为 lang=<fr|en>
查询参数传递.lang
参数后的部分,例如/cart
没有尾部斜杠,如果它是一个真正的目录,那么 /cart?lang=fr
将被重定向到 /cart/?lang=fr
由 Apache 的 mod_dir
模块提供,您的内部 URL 将在浏览器中公开.
In a later rule we remove lang
component from URL and pass it as lang=<fr|en>
query parameter. Part after lang
parameter e.g. /cart
doesn't have a trailing slash and if it is a real directory then /cart?lang=fr
will be redirected to /cart/?lang=fr
by Apache's mod_dir
module and your internal URL will be exposed in browser.
因此,在当前规则中,我们捕获 lang
组件之后的部分并检查我们是否没有尾部斜杠并且它是一个目录,然后此规则在内部重写为 /fr/cart/
带有斜杠.随后的规则将其重写为 /cart/?lang=fr
并且 mod_dir
不再重定向.
So in this current rule we capture part after lang
component and check if we don't have a trailing slash and it is a directory then this rule internally rewrites to /fr/cart/
with a trailing slash. Later rule then rewrites it to /cart/?lang=fr
and mod_dir
doesn't redirect anymore.
这篇关于重定向到除/amp/URL 之外的默认语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!