Apache开启rewrite重写模块
Apache重写规则
欢迎关注微信公众号“运维之美”,一个长姿势的公众号!
开启重启功能
apache开启重写,需要在httpd.conf文件中去除#,开启mod_rewrite模块
LoadModule rewrite_module modules/mod_rewrite.so
RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础的功能
实例1
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
上面语句的作用是当你是用FF浏览器访问index.php这个文件的时候,会自动让你访问到index.m.php这个文件,当你是用一些移动终端访问的 时候,会让你对index.php这个文件的访问实际访问的是index.L.php去,如果你是用其它的浏览器访问的时候,会让你跳到 index.b.php
实例2 http强转https
RewriteEngine on RewriteBase / RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
含义是这样的:为了让用户访问传统的http://转到https://上来,用了一下rewrite规则:
第一句:启动rewrite引擎
第三句:rewrite的条件是访问的服务器端口不是443端口
第四句:这是正则表达式,^是开头,$是结束,/?表示有没有/都可以(0或1个),(.*)是任何数量的任意字符
整句的意思是讲:启动rewrite模块,将所有访问非443端口的域名请求,url地址内容不变,将http://变成https://。
只是针对某一个url的https跳转
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} /beijing RewriteRule ^(.*$) https://bobo.kevin.com/beijing/ [R,L] </IfModule>
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{REQUEST_URI} !^/tz.php RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
%{SERVER_PORT} 说明访问端口
%{REQUEST_URI} 比如如果url是 http://localhost/tz.php,则是指 /tz.php
%{SERVER_NAME} 比如如果url是 http://localhost/tz.php,则是指 localhost
以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。这样便实现了:访问了 http://localhost/index.php 或者 http://localhost/admin/index.php 等页面的时候会自动跳转到 https://localhost/index.php 或者 https://localhost/admin/index.php,但是访问 http://localhost/tz.php 的时候就不会做任何跳转,也就是说 http://localhost/tz.php 和 https://localhost/tz.php 两个地址都可以访问。
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{SERVER_PORT} 443 RewriteRule ^(.*)$ https://blog.shibo.com/$1 [R=301,L] </IfModule>
其中R=301表示Moved Permanently,即告诉搜索引擎或者浏览器下去直接访问后者的地址,如果只是试验性地重定向,可以使用R=302(Found),临时跳转
rewrite跳转规则
Apache模块mod_rewrite提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求。它支持每个完整规则可以拥有不限数量的子规则以及附加条件规则的灵活而且强大的URL操作机制。此URL操作可以依赖于各种测试,比如服务器变量、环境变量、HTTP头、时间标记,甚至各种格式的用于匹配URL组成部分的查找数据库。
mod_rewrite模块可以操作URL的所有部分(包括路径信息部分),在服务器级的(httpd.conf)和目录级的(.htaccess)配置都有效,还可以生成最终请求字符串。此重写操作的结果可以是内部子处理,也可以是外部请求的转向,甚至还可以是内部代理处理。
以下重点介绍下RewriteRule 的规则以及参数说明。RewriteRule指令是重写引擎的根本。此指令可以多次使用。每个指令定义一个简单的重写规则。这些规则的定义顺序尤为重要——在运行时,规则是按这个顺序逐一生效的。
关注微信公众号:运维之美,获取更多运维知识,运维资料!
- Apache Rewrite模块重写功能开启及检测实现
- Ubuntu下apache rewrite模块开启
- openSUSE 11.2 开启 apache mod_rewrite 模块
- 开启Apache mod_rewrite模块完全解答
- 开启Apache mod_rewrite模块(解决404 Not Found)
- CentOs开启Apache的rewrite_module模块,支持.htaccess
- Apache2.4开启Rewrite模块
- Apache-rewrite模块重写相关的参数说明
- apache重写模块开启
- Apache如何开启Mod_rewrite模块以及Wordpress伪静态
- Apache模块详解-mod_rewrite重写模块
- 开启Apache mod_rewrite模块
- Apache Rewrite重写模块(一):一个入门的例子
- apache重写模块开启
- apache开启rewrite路径重写模式
- Ubuntu下Apache开启rewrite模块
- Apache模块 mod_rewrite 及 初级URL重写指南
- Apache Rewrite重写模块(二):10个实例详细分析
- zend framework 也可以不需要开启apache的 mod_rewrite 模块 虚拟主机 运行
- 【转】开启Apache mod_rewrite模块完全解答