apche 开启rewrite 重写url实现伪静态

本文详细介绍如何通过Apache服务器配置URL重写功能,包括启用rewrite模块、设置虚拟主机、修改hosts文件、创建.htaccess文件及定义重写规则等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先开启apache的rewrite模块  

LoadModule rewrite_module modules/mod_rewrite.so

2.开启支持虚拟主机并配置一个虚拟主机

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
DocumentRoot "d:/www2"
ServerName test.com
<Directory "d:/www2">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>

3.在C盘windows--system32-drivers-etc 找到hosts文件

配置:127.0.0.1    test.com

4.在项目的目录下新建 .htaccess 文件  内容如下:

 <IfModule mod_rewrite.c>
 RewriteEngine on  #开启重写
 RewriteRule ^news-id-(\d+)\.html news.php?id=$1    #重写的正则规则       要访问的真实地址及参数传递
 </IfModule>

5.重启apache    

访问url:   test.com/news-id-123456.html------->test.com/news.php?id=123456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值