基于目录的错误页面重定向

时间:2015-10-14 10:49:04

标签: .htaccess mod-rewrite vhosts

我的网站包含两个本地化内容,即EN和FR,内容将通过以下网址呈现:http://www.example.com/en/http://www.example.com/fr/

我想为每个区域设置实现不同的错误页面,但我不知道如何通过重定向/虚拟主机配置实现。

我读了每个目录的不同.htaccess文件,但是没有用。

帮助会很好。提前谢谢。

2 个答案:

答案 0 :(得分:0)

不同的.htaccess效果非常好,例如

/-+- en -- .htaccess
  |
  +- fr -- .htaccess

/en/.htaccess,你有

ErrorDocument 404 /path/to/en/error404.html

/fr/.htaccess

相同
ErrorDocument 404 /path/to/fr/error404.html

您还可以将错误文档放在同一目录中,并按扩展名区分,例如error404.en.htamlerror404.fr.html。那你有

ErrorDocument 404 /path/to/error404.en.html

ErrorDocument 404 /path/to/error404.fr.html

分别

如果您不想使用.htaccess文件,则可以使用DirectoryLocation指令实现相同的目标。在主要或虚拟主机配置中,这看起来像

<Directory "/path/to/example.com/en">
    ErrorDocument 404 /path/to/error404.en.html
</Directory>

<Directory "/path/to/example.com/fr">
    ErrorDocument 404 /path/to/error404.fr.html
</Directory>

<Location>指令看起来很相似。

答案 1 :(得分:0)

在Apache 2.4上,您可以使用表达式:

# for /fr/ URIs
<If "%{REQUEST_URI} =~ m#^/fr/#">
   ErrorDocument 404 /fr-404.html
</If>

# for /en/ URIs
<If "%{REQUEST_URI} =~ m#^/en/#">
   ErrorDocument 404 /en-404.html
</If>

Check Apache 2.4 expression reference

如果您仍然坚持使用Apache 2.2,那么您可以使用:

RewriteEngine On

# for /fr/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/ /fr-404.html [L]

# for /en/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/ /en-404.html [L]

但请记住,Apache 2.2解决方案会将200状态返回给客户端。