Apache 开启 Rewrite 模块



开启Rewrite模块

修改Apache主配置文件

.htaccess文件

RewriteEngine

RewriteCond参数

'-d' 是一个目录[Directory] '-f' 是一个常规的文件[File]

通过rewrite实现404定义

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]

RewriteRule 参数

伪静态操作

RewriteEngine On
RewriteRule ^/?news_(\d+).html news.php?nid=$1 [NC,L]

防止图片盗链

如果不是从本网站请求图片,替换成logo.png

RewriteEngine On
RewriteCond ${HTTP_REFERER} !^$
RewriteCond ${HTTP_REFERER} !192.168.1.100 [NC]
RewriteRule .(jpg|png)$ logo.png

直接返回403无权操作

RewriteEngine On
RewriteCond ${HTTP_REFERER} !^$
RewriteCond ${HTTP_REFERER} !192.168.1.100 [NC]
RewriteRule \.(gif|jpg|jpeg\png\bmp) - [F]

隐藏网站主入口文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php&1    
comments powered by Disqus