对无限循环和 RewriteCond 感到疯狂,需要帮助

发布于 2024-08-23 14:14:37 字数 1724 浏览 5 评论 0原文

如果用户输入不以“en”或“fr”开头的任何网址,我希望将用户重定向到我的主页。我花了几个小时试图完成这项工作,但这对于我脆弱的新手大脑来说太难了。目前,如果我尝试访问

http://mysite.com/fr/produits/

Firefox,就会告诉我重定向永远不会完成。

这就是我想要的:

http://mysite.com/en/whatever/ ==> rewrite as /whatever/index.php?lang=en
http://mysite.com/fr/whatever/ ==> rewrite as /whatever/index.php?lang=fr

http://mysite.com/jp/whatever/ ==> 301 redirect to /fr/accueil/ 
http://mysite.com/whatever/ ==> 301 redirect to /fr/accueil/ 

这是我当前的 htaccess。有关详细信息,请参阅内嵌注释。

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Require no www
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301]

# Redirect from root to /fr/accueil/, the URL for the default home page
RewriteRule ^/$ http://mysite.com/fr/accueil/ [NC,R=302]

# Rewrite language path fragment (/fr or /en) as a parameter 
RewriteRule ^fr/accueil/$ /accueil/index.php?lang=fr [QSA,NC] 
RewriteRule ^en/home/$ /accueil/index.php?lang=en [QSA,NC] 
RewriteRule ^fr/produits/$ /produits/index.php?lang=fr [QSA,NC] 
RewriteRule ^en/products/$ /produits/index.php?lang=en [QSA,NC] 

# I'm aware that the rules in this htaccess are re-examined each
# time a rewrite is issued. So the 'fr/accueil/' I'm redirecting to
# will itself be rewritten as /accueil/index.php?lang=fr. That is
# why I'm providing 3 conditions:
# If URI does not start with 'en' AND
# If URI does not start with 'fr' AND
# If QUERY_STRING is not exactly 'lang'
RewriteCond %{REQUEST_URI} !^en/.*$
RewriteCond %{REQUEST_URI} !^fr/.*$
RewriteCond %{QUERY_STRING} !^lang$
RewriteRule ^.*$ fr/accueil/ [NC,R=301]

请让我脱离痛苦。干杯!

I want to redirect users to my home page if they enter any URL not starting with 'en' or 'fr'. I've spent hours trying to make this work but this proves too tough for my fragile newbie brain. Currently, if I try to go to

http://mysite.com/fr/produits/

Firefox tells me that the redirect will never complete.

Here's what I want:

http://mysite.com/en/whatever/ ==> rewrite as /whatever/index.php?lang=en
http://mysite.com/fr/whatever/ ==> rewrite as /whatever/index.php?lang=fr

http://mysite.com/jp/whatever/ ==> 301 redirect to /fr/accueil/ 
http://mysite.com/whatever/ ==> 301 redirect to /fr/accueil/ 

Here's my current htaccess. See inline comments for details.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Require no www
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301]

# Redirect from root to /fr/accueil/, the URL for the default home page
RewriteRule ^/$ http://mysite.com/fr/accueil/ [NC,R=302]

# Rewrite language path fragment (/fr or /en) as a parameter 
RewriteRule ^fr/accueil/$ /accueil/index.php?lang=fr [QSA,NC] 
RewriteRule ^en/home/$ /accueil/index.php?lang=en [QSA,NC] 
RewriteRule ^fr/produits/$ /produits/index.php?lang=fr [QSA,NC] 
RewriteRule ^en/products/$ /produits/index.php?lang=en [QSA,NC] 

# I'm aware that the rules in this htaccess are re-examined each
# time a rewrite is issued. So the 'fr/accueil/' I'm redirecting to
# will itself be rewritten as /accueil/index.php?lang=fr. That is
# why I'm providing 3 conditions:
# If URI does not start with 'en' AND
# If URI does not start with 'fr' AND
# If QUERY_STRING is not exactly 'lang'
RewriteCond %{REQUEST_URI} !^en/.*$
RewriteCond %{REQUEST_URI} !^fr/.*$
RewriteCond %{QUERY_STRING} !^lang$
RewriteRule ^.*$ fr/accueil/ [NC,R=301]

Please put me out of my misery. Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-08-30 14:14:37

尝试将最后一行替换

RewriteRule ^.*$ fr/accueil/ [NC,R=301]

RewriteRule ^.*$ fr/accueil/ [NCL,R=301]

try replacing the last line

RewriteRule ^.*$ fr/accueil/ [NC,R=301]

With

RewriteRule ^.*$ fr/accueil/ [NCL,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文