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页面其他设置( 参考 )
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html #把通过二级目录访问的请求301重定向到二级域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]
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]
DirectoryIndex index.html index.cgi index.php
Header set article-by “yourdomain.com”
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”
#如果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 到这里结束
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 页面静态化/纯静态化/伪静态化
首先在项目根目录下新建文件,文件名为" .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.jpgRewriteEngine 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 页面静态化/纯静态化/伪静态化
相关文章推荐
- 用php编写空心金字塔,主要在于学习它的逻辑与思想
- PHP易混淆函数的区分方法及意义
- PHP 判断数组是否为空的方法
- 自己写的php curl库实现整站克隆
- php随机数怎么获取?一个简单的函数就能生成
- 关于UTF-8的BOM
- php-高级(文件处理和上次文件)
- php5.5环境搭建
- php-高级(多维数组,日期,include)
- LeapFTP3.0.1注册码
- 新的公司,新的技术
- PHP---注册界面的各种验证函数
- weiphp的学习
- PHP---运行时出现waring如何取消
- php curl 传递数据
- PHP中利用JS弹出错误信息乱码
- PHP,JAVA,NET 开发比较
- php递归获取顶级父类id
- php-表单操作
- php-基础知识(3)