我在 .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");
}
?>