php rewrite url_PHP实现url重写和.htaccess

.htaccess是一个完整的文件名(只有后缀),它是用于Apache服务器下的配置文件,当.htaccess文件放在某一文件夹下,它仅对该文件夹下的文件和文件夹有效。通过.htaccess文件,可以配置服务器实现很多功能,比如错误定位,密码保护,IP拒绝,URL重写等等。

默认的Apache不支持.htaccess,需要修改Apache的配置文件httpd.conf,才能使得.htaccess有效。

配置方法:

配置方面:

1. 找到apache的安装目录下的conf下的httpd.conf文件,打开文件修改

LoadModule rewrite_module modules/mod_rewrite.so这行代码,他前面有个#号,把#号删掉

2.  找到

Options FollowSymLinks ExecCGI Indexes

AllowOverride None

Order deny,allow

Deny from all

Satisfy all

这个节点,把None改为All.节点可能有多个,修改和PHP路径相关的那个。

3. 重启apache服务

接下来是创建.htaccess文件,并在里面写配置。Windows中新建文件的时候,不允许文件只有后缀,可以采用notepad等工具新建另存为该文件名。

如果要实现URL重写,配置文件中采用正则表达式是编写URL,并使之和常规的php文件映射。常用的写法如下:

RewriteEngine on                       //on为打开,off为关闭

RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$ b.php?action=$1&id=$2

RewriteRule ([a-zA-Z1-9]{1,})/([a-zA-Z1-9]{1,})$ a.php?controller=$1&action=$2

RewriteRule MyController/[a-zA-Z1-9]$ MyController.php?action=$1

ErrorDocument 404 /404.txt

网上找了一篇文件http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html例举了常用的5种映射,也可以参考。

product.php?id=12 to product-12.html

RewriteEngine on

RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

Rewriting product.php?id=12 to product/ipod-nano/12.html

RewriteEngine on

RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

Redirecting non www URL to www URL

RewriteEngine On

RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$

RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

RewriteEngine  On

RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1

RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

Redirecting the domain to a new subfolder of inside public_html.

RewriteEngine  On

RewriteCond %{HTTP_HOST} ^test\.com$ [OR]

RewriteCond %{HTTP_HOST} ^www\.test\.com$

RewriteCond %{REQUEST_URI} !^/new/

RewriteRule (.*) /new/$1

示例:

.htaccess文件内容如下

RewriteEngine on                       //on为打开,off为关闭

RewriteRule ^([a-zA-Z1-9]{1,})/([a-zA-Z1-9]{1,})$ a.php?controller=$1&action=$2

RewriteRule ^([a-zA-Z1-9]{1,})/([a-zA-Z1-9]{1,})/$ a.php?controller=$1&action=$2

说明:

正则表达式,严格匹配类似Controller/Action或者Controller/Action/,映射到a.php

a.php内容

echo "你的controller:".$_GET['controller']."
";

echo "你的action:".$_GET['action'];

?>

输入http://localhost:8080/Controller/Action/

则被解析到http://localhost:8080/a.php?controller=Controller&action=Action

这2个url是等价的。

注意,在映射url后加上查询字符串不影响正常的映射,比如输入http://localhost:8080/Controller/Action/?value=100,也是可以的。

参考文档:

http://www.htaccess-guide.com/

http://corz.org/serv/tricks/htaccess.php

http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279089.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

esp32 cam工作电流_我如何在家工作:Cam的生产力之痛

esp32 cam工作电流Telecommuting is becoming more and more common these days, with many tech writers (myself included) working from home on a full-time basis. I get asked about how I work fairly often, so here’s the skinny. 如今,远程办公变得越来越…

PL/SQL Developer结合oracle精简客户端配置说明

oracle 11g精简版客户端配置 ORACLE 一般情况下都会部署在服务器上,而在客户端上需使用PL/SQL Developer 等连接工具连接服务器上的 ORACLE 。 而此时往往需要在客户端上也安装上百兆的 ORACLE 然后通过 ORACLE 的网络配置向导来配置完成 PL/SQL Developer 的支持。…

NUMPY数据集练习 ----------SKLEARN类

123456<br><br># 1. 安装scipy&#xff0c;numpy&#xff0c;sklearn包import numpyfrom sklearn.datasets import load_iris# 2. 从sklearn包自带的数据集中读出鸢尾花数据集dataprint(data.data)123# 3.查看data类型&#xff0c;包含哪些数据data load_iris()pr…

java 伪异步 netty,大话netty系列之--伪异步BIO

生意规模扩大话说&#xff0c;老王和大明的生意越来越好&#xff0c;这就需要两个人增强业务往来&#xff0c;由于天南地北&#xff0c;两个人只能每次运输都需要雇一个人去运货(new 一个线程)&#xff0c;一个月下来&#xff0c;两人一算&#xff0c;人力成本太大了&#xff0…

如何使用Windows搜索在任何文件中搜索文本

Many of us rely on Windows Search to find files and launch programs, but searching for text within files is limited to specific file types by default. Here’s how you can expand your search to include other text-based files. 我们中的许多人都依赖Windows搜索…

php算法求出兔子数列,PHP算法:斐波那契数列的N种算法

前言前段时间&#xff0c;遇到优化计算斐波那契数列的常规递归方法&#xff0c;但是一时间并没有及时想到很好的方法&#xff0c;所以后面查找了相关资料&#xff0c;总结了多种计算解法&#xff0c;所以分享出来&#xff0c;和大家一起交流学习。斐波那契数是什么斐波那契数列…

.net core MongoDB 初试

是这样的&#xff0c;我们有一个场景&#xff0c;另一个服务器是写到MongoDB里面&#xff0c;我们的MVC页面要展示&#xff0c;需要分页展示 自己写了一个DAL public class MongoConnect{public string ConnectString { get; set; }}public class MongoBaseDAL<TEntity>{…

Linux文件和目录权限:chmod、更改所有者和所属组:chown,umask命令,隐藏权限:lsattr/chattr...

文件和目录权限chmod&#xff1a; 我们使用ls -l可以看到文件的详细信息&#xff0c;也知道第一列的第一个符号(字母)表示文件的类型&#xff0c;在表示文件的类型符号的后面的九个符号则表示的是文件的权限&#xff0c;这些权限和文件的所有者和所属组都有关系&#xff1a; 文…

【技术累积】【点】【java】【27】@JSONField

JSONField 该注解隶属于阿里fastjson&#xff0c;方便fastjson处理对象时的一些操作 源码 Retention(RetentionPolicy.RUNTIME) Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) public interface JSONField {/*** config encode/decode ordinal* s…

百度php editor图片上传到其他盘,百度编辑器Editor图片独立上传

将百度编辑器中的图片独立出来上传&#xff1a;html:代码var myEditorImage,d,myEditorImage new UE.ui.Editor();myEditorImage.render(uploadid);myEditorImage.ready(function(){myEditorImage.setDisabled();myEditorImage.hide();//隐藏UE框体myEditorImage.addListener(…

感谢支持,超预期重印并加码

今天&#xff0c;要向广大读者朋友带来一个&#xff0c;连我自己和出版社都感到十分意外的好消息&#xff0c;几天前接到出版社的通知&#xff0c;说今年元月出版的《Cisco/H3C交换机配置与管理完全手册》&#xff08;第二版&#xff09;马上就要下单重印了&#xff0c;而且一下…

如何从手机远程控制uTorrent

You’re a geek on the go and it’s important to keep tabs on your torrents when you’re away from home. Today we take a peak at how you can monitor, manage, and even start your torrent downloads when you’re away from your computer. 您是旅途中的怪胎&#x…

洛谷P2463 Sandy的卡片【后缀数组】【二分】

题目描述 Sandy和Sue的热衷于收集干脆面中的卡片。 然而&#xff0c;Sue收集卡片是因为卡片上漂亮的人物形象&#xff0c;而Sandy则是为了积攒卡片兑换超炫的人物模型。 每一张卡片都由一些数字进行标记&#xff0c;第i张卡片的序列长度为Mi&#xff0c;要想兑换人物模型&#…

php获取一个文件名的函数,PHP 文件系统函数之获取文件名及文件名后缀-php文件...

获取文件名(包含扩展):1.用PHP 文件函数 basename获取例&#xff1a;$filename "/home/httpd/html/index.php";$file basename($filename);2.先获取位置再获取文件名例:$filename "/home/httpd/html/index.php";$pos strrpos($filename, /);if ($pos …

tasker使用手册_如何开始使用Tasker调整Android手机

tasker使用手册Tasker is a powerful app for Android that lets you customize how your phone works and automate tasks. Unfortunately, it’s got a bit of a learning curve. We’re here to show you how to get started and turn your phone into a flashlight in the …

iPhone 软件:xlate free 编码的好帮手!

功能菜单&#xff1a; 1 文本 2 二进制 3 Char 值 4 Base64 5 反向 如果需要把一段中文编码请选择UTF16&#xff0c;如果是英文就选择UTF8。对于需要经常使用编码切换的朋友是个好帮手。 也可以用来简单加密&#xff1a;我们先在文本状态下输入一段不想让别人知道或需要保密的文…

linkbox php,win10 docker-toolsbox 搭建php开发环境的教程

下载镜像docker pull mysql:5.7docker pull php:7.2-fpmdocker pull nginxdocker pull redis:3.2设置共享文件宿主机创建目录E:\wnmp\mysql57\confE:\wnmp\mysql57\logE:\wnmp\php72\confE:\wnmp\php72\confE:\wnmp\nginx\confE:\wnmp\nginx\confE:\wnmp\wwwvmware设置文件共享…

sublime text3:提示 There are no packages available installation 解决方案

纯属记录&#xff0c;下次能找到解决。 第一步&#xff1a; 在sublime Text3界面按 ctrl 出现一个输入框界面 第二步&#xff1a;在输入框输入&#xff1a; import urllib.request,os,hashlib; h eb2297e1a458f27d836c04bb0cbaf282 d0e7a3098092775ccb37ca9d6b2e4b7d; pf Pa…

如何提取幻灯片表格_如何查看对Google文档,表格或幻灯片文件的最新更改

如何提取幻灯片表格The Google Suite offers you a handy way to view all the changes that have occurred in a file on Google Docs, Sheets, or Slides. This is extremely useful when you’ve made lots of changes to a file or are working as part of a team and need…

[20171130]关于rman的一些总结.txt

[20171130]关于rman的一些总结.txt --//最近一直做rman相关测试,测试那个乱,没办法.无法从周围的人获得帮助,纯粹是自己的乱猜,乱测,不知道别人是否能看懂我写的东西. --//有必要做一些总结,不一定对,仅仅是我当前的看法. 1.数据文件备份集中,文件头是最后写到备份集文件的. 2.…