略微加速

略速 - 互联网笔记

Apache中RewriteCond规则参数理解(如果文件不存在则rewrite)

2018-08-29 leiting (2377阅读)

标签 运维

RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础的功能,为了方便理解,下面来看看几个例子。
RewriteEngine on
RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla\/5\.0.*
RewriteRule  index.php            index.m.php

RewriteCond  %{HTTP_USER_AGENT}  ^Lynx.*
RewriteRule  index.php            index.L.php 

RewriteRule  index.php            index.b.php

 

RewriteCond %{HTTP_REFERER} (www.test.cn)
RewriteRule (.*)$ test.php
上面语句的作用是如果你访问的上一个页面的主机地址是www.test.cn,则无论你当前访问的是哪个页面,都会跳转到对test.php的访问。

 


RewriteCond %{REMOTE_HOST} ^host1.* [OR]
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
RewriteCond %{REMOTE_HOST} ^host3.*
RewriteRule (.*)$ test.php
上面语句的作用是如果你的地址是host1或host2或host3的时候,则就跳到对test.php。从这里可以看出,RewriteCond语句之间默认的是AND,如果想要OR,则要明确的写出来。

下面是自己收藏的一些有用的重写规则:
RewriteCond %{REQUEST_FILENAME} !-f   //如果文件存在,就直接访问文件,不进行下面的RewriteRule.(不是文件或文件不存在就执行重写)

RewriteCond %{REQUEST_FILENAME} !-d   //#如果目录存在就直接访问目录不进行RewriteRule

RewriteCond %{REQUEST_URI} !^.*(\.css|\.js|\.gif|\.png|\.jpg|\.jpeg)$ //#如果是这些后缀的文件,就直接访问文件,不进行Rewrite


https://blog.csdn.net/clj9017/article/details/11250067

########################################################
LoadModule jk_module modules/mod_jk.so
Include conf/mod_jk.conf
########################################################
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/svcroot/runtime/webstatic/shanhyweb"
ServerName shanhyweb.example.com
#ServerAlias www.shanhyweb.example.com
ErrorLog "logs/shanhyweb-error_log"
CustomLog "logs/shanhyweb-access_log" common
<IfModule mod_jk.c>
#日志输出文件(其他配置也可以重写mod_jk.conf里面的配置)
JkLogFile logs/mod_jk_shanhyweb.log
#指URL指向如果有servlet,则让worker_web去处理
JkMount /servlet/* worker_web
#指URL为/*.jsp的页面,让worker_web去处理
JkMount /*.jsp worker_web
#指URL为/*.do的页面,让worker_web去处理
JkMount /*.do worker_web
#指URL为/*.json的页面,让worker_web去处理
JkMount /*.json worker_web
</IfModule>
	<IfModule mod_rewrite.c>
		RewriteEngine On
		# '-s' (is regular file, with size)
		# '-l' (is symbolic link)
		# '-d' (is directory)
		# 'ornext|OR' (or next condition)
		# 'nocase|NC' (no case)
		# 'last|L' (last rule)
		# 'QSA' 追加请求字符串
		RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-s
		RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
		RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
		RewriteRule ^(.*)\.(jpg|png|bmp)$ http://shanhyweb.example.com/createimage.jsp?path=%{REQUEST_URI} [NC,L]
	</IfModule>
<Directory "/svcroot/runtime/webstatic/shanhyweb">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>



RewriteEngine	On
RewriteCond	%{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-s
RewriteRule	^([\w\_\-\/\.]*)$	/index.php


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3