Apache伪静态配置

一、Rewrite规则简介: Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言。可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式进行设置。 二、在Apache配置中启用Rewrite 打开配置文件httpd.conf:

**1.启用rewrite**
# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的 #
2.启用.htaccess
在虚拟机配置项中
AllowOverride None    修改为: AllowOverride All   //让apache服务器支持.htaccess

也可以直接在http.conf 中配置

<VirtualHost *:8080>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "D:/phpwin/wamp/www/yktadmin/public"
    ServerName qrcodepay.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
	
	<Directory  "D:/phpwin/wamp/www/yktadmin/public">
        Options FollowSymLinks ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
		Require local
           RewriteEngine on
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond ${REQUEST_FILENAME} !-d
           RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
	    </Directory>
</VirtualHost>

二、Rewrite基本写法 服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件。 代码如下

RewriteEngine on    //启动rewrite引擎
RewriteRule ^/index([0-9]*).html$ /index.php?id=$1   //“([0-9]*)” 代表范围 用(.*)代表所有,下同。
RewriteRule ^/index([0-9]*)/$ /index.php?id=$1 [R]   //虚拟目录

4.rewrite规则学习
在新建.htaccess文件之后,就在里面写入以下内容: RewriteEngine on #rewriteengine 为重写引擎开关on为开启off为关闭 RewriteRule ([0-9]{1,})$index.php?id=$1 在这里,RewriteRule是重写规则,是用正则表达式的句子, ([0-9]{1,})表示由数字组成的,$表示结束标志,表示以数字结束!如果要实现伪静态页面,规则如下: RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2 在为个正则表达式中, ([a-zA-Z]{1,})-([0-9]{1,}).html$是规则,

index.php?action=$1&id=$2是要替换的格式,$1代表第1括号匹配的值,$2代表第二个括号的值,如此类推! 测试PHP脚本如下: index.php文件中的代码如下: echo ‘你的Action值为:’ . $_GET['action']; echo ‘ ’; echo ‘ID值为:’ . $_GET['id']; ?>

在浏览器地址栏输入: localhost/page-18.html 输出的是: 你的Action值为:page ID值为:18

三、Apache mod_rewrite规则重写的标志一览

  1. R[=code](force redirect) 强制外部重定向 强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
  2. F(force URL to be forbidden)禁用URL,返回403HTTP状态码。
  3. G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。
  4. P(force proxy) 强制使用代理转发。
  5. L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
  6. N(next round) 重新从第一条规则开始运行重写过程。
  7. C(chained with next rule) 与下一条规则关联 如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。
  8. T=MIME-type(force MIME type) 强制MIME类型
  9. NS (used only if no internal sub-request) 只用于不是内部子请求
  10. NC(no case) 不区分大小写
  11. QSA(query string append) 追加请求字符串
  12. NE(no URI escaping of output) 不在输出转义特殊字符 例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zoo
  13. PT(pass through to next handler) 传递给下一个处理 四、Apache rewrite例子 例子一: 同时达到下面两个要求: 1.用http://www.jb51.net/xxx.php 来访问 http://www.jb51.net/xxx/ 2.用http://yyy.jb51.net 来访问 http://www.jb51.net/user.php?username=yyy 的功能
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.jb51.net
RewriteCond %{REQUEST_URI} !^user.php$
RewriteCond %{REQUEST_URI} .php$
RewriteRule (.*).php$ http://www.jb51.net/$1/ [R]
RewriteCond %{HTTP_HOST} !^www.jb51.net
RewriteRule ^(.+) %{HTTP_HOST} [C]
RewriteRule ^([^.]+).jb51.net http://www.jb51.net/user.php?username=$1

例子二

/type.php?typeid=* –> /type*.html
/type.php?typeid=*&page=* –> /type*page*.html
RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]

转载于:https://my.oschina.net/u/2299514/blog/796068

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值