ErrorDocument 404 .htaccess无法使用重写规则301进行规范

时间:2014-10-11 00:28:27

标签: .htaccess redirect rewrite http-status-code-404 errordocument

我在.htaccess文件中使用以下代码来整理Apache服务器中的规范重定向。这非常有效,并将example.com,example.com / index.html和www.example.com/index.html ALL重定向到www.example.com: -

#Redirect to www location
#Redirect example.com to www.example.com
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteRule ^index\.(php|html?)$ http://www.example.com/ [L,R=301]

以上代码在.htaccess中完美运行

我还希望有一个ErrorDocument 404规则,将到达不存在页面的访问者发送到特别设计的页面,即:http://example.com/404/404.html

然而,当我将以下行添加到我的.htaccess文件时,它什么也没做。即导航到不存在的页面(例如,输入URL www.example.com/asidhaskudhsadh只返回标准404错误页面)。我试图使用的代码是:

ErrorDocument 404 http://www.example.com/404/404.html

(Ps。这不是绝对的URL地址问题,因为我经常尝试使用

ErrorDocument 404 http://www.google.com 

它仍然不起作用。无论我是否包含ErrorDocument行,规范重定向继续工作。 ErrorDocument行基本上似乎没有任何效果。

有什么想法吗?我的整个.htaccess文件代码如下所示。底部的奇怪线路由外部移动网站开发团队提供,以检查访问者是否在移动设备上,并且应该访问网站的移动版本(即http://www.example.com/m)。它引用的index.php文件也包含在下面。

.htaccess文件

AddType x-mapp-php5 .php

ErrorDocument 404 http://www.example.com/404/404.html

#Redirect example.com to www.example.com
#Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteRule ^index\.(php|html?)$ http://www.example.com/ [L,R=301]


##### Speed Up Loading With Caching of Files #####
<IfModule mod_headers.c>
# YEAR
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
Header set Cache-Control "max-age=2419200"
</FilesMatch>
# WEEK
<FilesMatch "\.(js|css|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 45 MIN
<FilesMatch "\.(html|htm|txt)$">
Header set Cache-Control "max-age=2700"
</FilesMatch>
</IfModule>

###### DO NOT REMOVE THIS LINE!!! ######
DirectoryIndex index.php
###### DO NOT REMOVE THIS LINE!!! ######

桌面网站使用索引加载。 html

.htaccess文件最后一行中引用的索引。 php 文件代码如下:

<?php
ob_start( 'ob_gzhandler' );
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

/* recomment in for mobile activation */
//echo @file_get_contents("index.html");

if(isset($_GET["m"]) && $_GET["m"] == "off"){
    $expire = time() + 60 * 60 * 24;
    setcookie("mobile", "off", $expire);
    echo @file_get_contents("index.html");
} else if (isset($_COOKIE["mobile"]) && $_COOKIE["mobile"] == "off"){
    echo @file_get_contents("index.html");
} else if ($iphone || $android || $palmpre || $ipod || $berry == true){ 
    header("Location: http://www.example.com/m/home.php");
} else {
    echo @file_get_contents("index.html");
}

?>

0 个答案:

没有答案