【概述】URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是index.php?type=news&&cata=sport&&id=123那么它可以被重写成 “news-sport-123.html”,这样的URL,这样的网址可以更好的被搜索引擎抓取。

  【优点】URL重写可以降低使用动态网页时受sql注入攻击的几率,还可以在不静态化的网页的情况下,利于搜索引擎对网页的抓取(SEO)。

  【配置】

  1,启用httpd.conf配置文件中的rewrite静态库

#开启apache rewrite重写模块
LoadModule rewrite_module modules/mod_rewrite.so

  2,配置测试虚拟主机

  2.1,启用httpd.conf配置文件中的httpd-vhosts.conf选项

# Virtual hosts 开启此选项,使其包含httpd-vhosts.conf配置文件
Include conf/extra/httpd-vhosts.conf

  2.2在httpd-vhosts.conf配置文件中添加一个虚拟主机

<VirtualHost *:80>
DocumentRoot "X:/phptest/urlrewrite_test"
ServerName www.gazeon.com
#Directory这个段,主要用处指定,该目录的访问权限
<Directory "X:/phptest/urlrewrite_test">
#允许所有人访问
Allow from all #不列出该网站的文件列表
Options None #设置网站默认的首页面 http://www.gazeon.com
DirectoryIndex index.php #配置统一的 404页面
errordocument 404 /error404.html #开启重写引擎
RewriteEngine On
#配置url重写规则 ,使用正则匹配,$1,$2,$3表示第N个子表达式中匹配的结果
RewriteRule (\w+)-(\w+)-(\d+)\.html$ index.php?type=$1&&cata=$2&&id=$3 </Directory>
</VirtualHost>

  2.2.1【注意】如果你的apache安装目录没有httpd-vhosts.conf文件,可以将以上配置内容直接写到httpd.conf配置文件中。

  2.2.2【另外】你也可以在需要访问的文件目录下,直接通过写.htaccess文件来规范重写规则,比在httpd.conf文件中配置更灵活

#.htaccess文件内容
<IfModule rewrite_module>
#开启重写引擎
RewriteEngine On
#配置url重写规则 ,使用正则匹配,$1,$2,$3表示第N个子表达式中匹配的结果
RewriteRule (\w+)-(\w+)-(\d+)\.html$ index.php?type=$1&&cata=$2&&id=$3
</IfModule>

  重启Apache。  

  3,在hosts文件中添加规则(可选)

  打开C:\Windows\System32\drivers\etc 目录下的hosts文件,添加如下代码

127.0.0.1/urlrewrite_test www.gazeon.com

  4,建立测试网也页

  在X:\phptest\urlrewrite_test 下新建index.php

<?php
/*
2016/3/10 YunGaZeon
此网页仅用于URL重写测试,因此尽量简单
*/
echo '<pre>';
print_r($_GET);
echo '</pre>';

  4.1浏览器打开www.gazeon.com/news-sport-123.html 测试查看

  显示以下内容,则配置成功

Array
(
[type] => news
[cata] => sport
[id] => 123
)

  

  

  

【Apache】apache简单配置URL重写规则的更多相关文章

  1. windows Apache服务器简单配置虚拟域名(转载)

    1.找到apache目录下的conf下的extra下的httpd-vhosts.conf虚拟主机配置文件 将下面的代码复制粘贴到最下面:   #<VirtualHost *:80>#   ...

  2. Apache Rewrite url重定向功能的简单配置

    http://www.jb51.net/article/24435.htm 1.Apache Rewrite的主要功能 就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我 ...

  3. Apache URL重写规则

    1.简介 Apached的重写功能,即是mod_rewrite模块功能,它是apache的一个模块.它的功能非常强大,可以操作URL中的所有部分. 因此我们就可以改写url,给用户提供一个简介大方的u ...

  4. Apache服务器的简单配置与安全策略

    在之前讲的关于weevely后门分析中,有说到利用Apache的配置文件.htaccess来隐藏php后门.关于.htaccess文件的用途,在此结合Apache服务器的具体配置作一详解,也算是自己的 ...

  5. asp.net 网站在Apache下的配置,就这么简单

    asp.net 网站在Apache下的配置,就这么简单 # # Virtual Hosts # # If you want to maintain multiple domains/hostnames ...

  6. asp.net 站点在Apache下的配置,就这么简单

    asp.net 站点在Apache下的配置,就这么简单 # # Virtual Hosts # # If you want to maintain multiple domains/hostnames ...

  7. windows下简单配置apache

    不得不做个笔记,不然每次配置都记不清楚... 详细的配置朋友这边写的很好.地址 # 对 PHP 4 LoadModule php4_module "c:/php/php4apache2.dl ...

  8. Apache配置URL重定向

    Apache配置URL重定向 修改/etc/httpd/conf/httpd.conf文件的内容 <Directory "/var/www"> AllowOverrid ...

  9. Linux中Apache服务器的简单配置

    配置Apache服务器: 1.安装Apache,可以参考Linux 中yum的配置来安装: yum install http* -y 2.修改SELinux: setenforce 0 查看: 3.防 ...

随机推荐

  1. hdu1181 变形课

    Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个 ...

  2. Codeforces #Round 376 部分题解

    A: 题目传送门:http://codeforces.com/problemset/problem/731/A 直接根据题意模拟即可 #include "bits/stdc++.h" ...

  3. Android --自定义简单Toast

    1. 效果图

  4. String之“==”与equals

    有时候String类型用“==”判断相等时无法成功,经过实验,用string.equals方法可以判断成功!! for (int i = 0; i < 10000; i++) {   Strin ...

  5. Hibernate映射一对多双向关联关系及部门关联属性

    一对多双向关联关系:(Dept/Emp的案例) 既可以根据在查找部门时根据部门去找该部门下的所有员工,又能在检索员工时获取某个员工所属的部门. 步骤如下: 1.构建实体类(部门实体类加set员工集合) ...

  6. [LintCode] Toy Factory 玩具工厂

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  7. [CareerCup] 18.6 Smallest One Million Numbers 最小的一百万个数字

    18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume t ...

  8. orcale 动态执行语句

    create or replace function fn_test2(tablename in varchar2) return number is rtn number; begin --通用的获 ...

  9. Logistic Regression vs Decision Trees vs SVM: Part II

    This is the 2nd part of the series. Read the first part here: Logistic Regression Vs Decision Trees ...

  10. CSS重新认识(一)

    1. 所有的元素都遵循盒子模型,即内容部分+padding(填充部分)+border+margin(外边距部分); 2.我们平常定义的width与height指的内容部分的长宽; 3. 行内元素在不改 ...