多国语言的自定义404 htaccess的规则导致重定向循环自定义、多国语言、重定向、规则

2023-09-02 09:59:26 作者:ゅ 空城空旧忆。

我工作的一个多语言的网站,我需要建立一个自定义404错误页面为每种语言。我得在.htaccess以下规则,不太工作的权利:

I'm working on a multi language website where I need to set up a custom 404 page for each language. I've got the following rules in .htaccess which don't quite work right:

RewriteCond %{REQUEST_URI} !^/(ie)(/|$) [NC]
ErrorDocument 404 http://www.domain.com/ie/404/

RewriteCond %{REQUEST_URI} !^/(se)(/|$) [NC]
ErrorDocument 404 http://www.domain.com/se/404/

RewriteCond %{REQUEST_URI} !^/(nl)(/|$) [NC]
ErrorDocument 404 http://www.domain.com/nl/404/

#last rule becomes default
RewriteCond %{REQUEST_URI} !^/(en)(/|$) [NC]
ErrorDocument 404 http://www.domain.com/uk/404/

RewriteRule ^([a-z]{2})/404(/)?$ index.php?controller=utils&method=view404&lang=$1 [L]
RewriteRule ^([a-z]{2})/404.html$ index.php?controller=utils&method=view404&lang=$1 [L]

我认为这个问题可能与!在RewriteCond指令,但是除去这并没有帮助。如果我访问domain.com/4t3409t0(不存在的页面),这符合过去的RewriteCond并重定向到domain.com/uk/404(其中实际工作)。

I think the issue may be with the ! in the RewriteCond, however removing this didn't help. If I visit domain.com/4t3409t0 (a page which doesn't exist) this matches the last RewriteCond and redirects to domain.com/uk/404 (which actually works).

但是,如果我尝试这样的URL domain.com/ie/wfnwio它试图重定向到domain.com/ie/404~~V(因为它应该),我会卡在重定向循环。

However if I try a URL such as domain.com/ie/wfnwio it attempts to redirect to domain.com/ie/404 (as it should) and I get stuck in a redirect loop.

所以看起来当最后的RewriteCond满足改写作品,但像任何其他人,他们失败。

So it looks like when the last RewriteCond is met the rewrite works but for any others they fail.

我只需要设置的ErrorDocument网址为每种语言,已经存在了重定向不存在的内容,以404的功能。

I just need to set the ErrorDocument URL for each language, the functionality for redirecting non existing content to 404 already exists.

感谢您的任何输入,

詹姆斯

推荐答案

我想这个问题我是用的RewriteCond是不正确。不过,我发现了一个解决方法,因为PHP存储在一个会话变量中的语言。

I think the issue I had was with the RewriteCond being incorrect. However I found a workaround as PHP stores the language in a session variable.

ErrorDocument 404 http://www.domain.com/404/

RewriteRule ^404(/)?$ index.php?controller=utils&method=view404 [L]
RewriteRule ^([a-z]{2})/404(/)?$ index.php?controller=utils&method=view404&lang=$1 [L]
RewriteRule ^([a-z]{2})/404.html$ index.php?controller=utils&method=view404&lang=$1 [L]

我的解决方法只是简单地使用domain.com/404作为默认,如果可能的话,然后设置了语言,通过会话,或加载英国404页,如果没有。

My workaround simply uses domain.com/404 as the default, which then sets the language via session if possible, or loads the UK 404 page if not.

如果语言变量是在查询字符串设置它使用的是第二代和第三重写规则过去了。

If the language variable is set in the query string it is passed using the 2nd and 3rd rewrite rules.