apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置。通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向、自定义400/403/404/500错误页面、改变文件扩展名、允许/阻止指定IP用户访问、禁止目录列表、配置默认文档等功能,可以说是功能非常强大,下面就给大家介绍一下最常用的几个功能的设置方法。
设置网站错误页面
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
设置网页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
apache 网页301重定向、自定义400/403/404/500错误页面的更多相关文章
- Django创建自定义错误页面400/403/404/500等
直接参考: https://zhuanlan.zhihu.com/p/38006919 DEBUG =True的话,为开发环境,显示不了404页面.
- 网站404,500错误页面的处理,及500异常写入errorLog日志
1.web.xml 配置 <error-page> <error-code>404</error-code> <location>/404.jsp< ...
- Tomcat更改错误页面指向,改变404,500错误页面
在公司工作了一段时间,也被安排做了一个App,而且后台也是我来写和布置的,由于一次安全检查,需要我把tomcat默认页(管理页面)关闭,于是我只能进行默认指向变更,但是后面我又想到要是用户输入不存在的 ...
- Spring MVC自定义403,404,500状态码返回页面
代码 HTTP状态码干货:http://tool.oschina.net/commons?type=5 import org.springframework.boot.web.servlet.erro ...
- HTTP/1.0中,状态码200 301 304 403 404 500的含义?
200 OK 服务器成功处理了请求 301 重定向,请求的URL已移走 304未修改,客户的缓存资源是最新的,要客户端使用缓存 403禁止,请求被服务器拒绝了 404未找到资源 500内部服务器错误, ...
- 整理ASP.NET MVC 5各种错误请求[401,403,404,500]的拦截及自定义页面处理实例
http://2sharings.com/2015/asp-net-mvc-5-custom-404-500-error-hanlde https://blog.csdn.net/yhyhyhy/ar ...
- ***apache做301重定向的方法
将不带www的定向到带www去 方法一:加在httpd.conf 1.这里我使用mod_rewrite重写URL的方式来做,做之前朋友记得检查一下你的apache是否已经加载了rewrite模块.如图 ...
- 自定义 404 与 500 错误页面,URL 地址不会重定向(一)
对于 404 与 500 错误发生时,我们希望自己定义一个更加人性化的页面. 例子 当访问下面这个地址时: http://localhost/aaaa/bbb/ccc/ddd/eee/fff/ggg ...
- 自定义 404 与 500 错误页面,URL 地址不会重定向(二)
上一篇是使用了全局过虑器来实现,还可以使用 HttpApplication 来处理. 参考文章: http://www.cnblogs.com/dudu/p/aspnet_custom_error.h ...
随机推荐
- OFV.msi是什么 为什么更新时无法安装
在网络上搜索了下 这个是Microsoft Office 文件验证加载项,微软提供了一个单独的文件:http://www.microsoft.com/downloads/zh-cn/confirmat ...
- 安装XAMPP时出现 unable to realloc 83886080 bytes
参考:https://www.apachefriends.org/faq_linux.html 转载:http://blog.xinspace.space/2015/08/06/xampp-unabl ...
- java基础篇1之可变参数,增强for循环,自动装箱
1.java中可变参数应用 例如 add(int x,int... args) 1)只能放在参数列表的最后面 2)...位于变量类型和变量名之间,前后有无空格都可以 3)调用可变参数的方法时,编译器为 ...
- GCC编译笔记
需要移植一个vs2008的项目到linux上,代码比较复杂,重新写比较困难,于是开始折腾 首先移植到codeblocks上,sprints_s这类的vs函数都要改,windows调用要改 编译通过 c ...
- 转: JDK包含的基本组件
JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK.JDK 是整个Java的 ...
- vim g s 替换区别
vim g s 替换区别 PS:一篇好文收藏备用,今天用它解决了一个大问题. 发信人: vale (浅谷), 信区: VIM标 题: global命令详解 发信站: 水木社区 (Fri Ju ...
- 微信小程序 - 考试前三排名实现
实现原理:利用背景图片以及nth-child实现
- maximal-rectangle——找出最大矩形的面积
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- iOS 瀑布流封装
代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...
- kettle转换之多线程
kettle转换之多线程 ETL项目中性能方面的考虑一般是最重要的.特别是所讨论的任务频繁运行,或一些列的任务必须在固定的时间内运行.本文重点介绍利用kettle转换的多线程特性.以优化其性能. ...