happyxp 发表于 2019-5-13 09:29:10

Apache如何开启伪静态

环境:系统 Windows+Apache 2.2

加载Rewrite模块:
在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。

允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All

在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。

Apache Rewrite模块的简单应用:
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。

1、请求跳转
目的是如果请求为.jsp文件,则跳转至其它域名访问。
例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页
注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。
修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容
RewriteEngine on
#开启Rewrite模块
RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写
2、域名跳转
如果请求为old.xp6.org下的所有URL,跳转至b.
RewriteEngine on
#开启Rewrite模块
RewriteCond %{REMOTE_HOST} ^old.xp6.org$
#针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
RewriteRule (.*) http://b.xp6.org/$1 3、防盗链
如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
RewriteEngine on
#开启Rewrite模块
RewriteCond %{HTTP_REFERER} !^$
#如果不是直接输入图片地址
RewriteCond %{HTTP_REFERER} !img.xp6.org$
#且如果不是img.xp6.org所有子域名调用的
RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$
RewriteCond %{HTTP_REFERER} !qq.com
RewriteCond %{HTTP_REFERER} !google.com
RewriteCond %{HTTP_REFERER} !google.cn
RewriteCond %{HTTP_REFERER} !baidu.com
RewriteCond %{HTTP_REFERER} !feedsky.com
RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://xp6.org/err.jpg 4、不需要定义.htaccess文件
在Apache2\conf\httpd.conf 最后一行添加
RewriteEngine On
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2重启Apache,登陆后台开启全伪

Linux+Apache环境配置类似。

页: [1]
查看完整版本: Apache如何开启伪静态