您的位置:首页 > 编程语言 > PHP开发

PHP 404页面/如何设置404页面/URL静态化/URL伪静态化

2015-08-01 13:11 671 查看
php中如何设置404页面及其他错误页面

首先在项目根目录下新建文件,文件名为" .htaccess "

在该文件中写入一下配置项:

ErrorDocument 404 /404.html 或者是带有文件路径的地址: ErrorDocument 404 /error_pages/404.html

其他 401、500 等错误同理

注意点是:( 参考 )

  不要将错误页面指向主页面,可能会导致主页在搜索引擎中消失;

  错误页面地址设置用相对路径(如果有绝对地址,会返回发起两次请求,第一次是302重定向,重定向到你设置的绝对地址页面,该页面返回200)(自测)

.htaccess页面其他设置( 参考 )

设置网页301重定向

#从 old_dir 目录重定向到 new_dir 目录
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html #把通过二级目录访问的请求301重定向到二级域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1

禁止指定IP段用户的访问

#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

禁止指定来源网页访问

#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]

图片防盗链设置

#从本站以外的域名访问图片,一律显示 feed.jpg
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]

设置文件夹首页

#防止显示文件夹列表,当访问文件夹时,服务器会查找index.html,并将其做为首页文件,如不存在依次向后查找
DirectoryIndex index.html index.cgi index.php

设置多媒体文件为可下载而非播放

AddType application/octet-stream .mp3 .mp4

自定义HTTP报头

Header set X-Pingback “http://www.yourdomain.com/xmlrpc.php”
Header set article-by “yourdomain.com”

设置文件过期时间 Cache Control

# 启用有效期控制
ExpiresActive On
# gif/png/jpg 有效期为1个月
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
# js/css 有效期为1星期
ExpiresByType text/javascript “access plus 1 week”
ExpiresByType text/css “access plus 1 week”

WordPress建站程序伪静态代码

# BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始
#如果Apache加载了mod_rewrite.c模块,则运行以下代码
RewriteEngine On #启用 mod_rewrite 引擎
RewriteBase / #设置目录重写的基准URL为 /
RewriteRule ^index\.php$ – [L] #如果请求路径是 index.php,停止重写操作(避免死循环)
RewriteCond %{REQUEST_FILENAME} !-f #如果请求的不是一个文件,继续处理
RewriteCond %{REQUEST_FILENAME} !-d #如果请求的不是一个目录,继续处理
RewriteRule . /index.php [L] #把所有的请求指向 /index.php
#结束 IfModule
# END WordPress #WordPress 的 htaccess 到这里结束

Discuz x3/x3.1通用伪静态代码

#如果Apache加载了mod_rewrite.c模块,则运行以下代码
RewriteEngine On
RewriteBase /discuz
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1
#结束 IfModule

URL静态化/URL伪静态化

参考http://blog.csdn.net/shuiaaa/article/details/6561793

也可以参考我的另外一篇文章:PHP 页面静态化/纯静态化/伪静态化
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: