url rewriting index.php(url重写index.php)
问题描述
我有类似的网址
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
但我想要像这样的网址
http://mysite.com/resources
http://mysite.com/resources/view/938
与其制定数百个重写规则,我想知道是否有可能只有一个?我认为这是可能的,方法是获取 uri 并将其拆分为多个部分",然后只需为 index.php 添加重写规则
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
但是怎么样?有人可以举个例子或链接一个教程
but how? could someone give an example or link a tutorial
推荐答案
我碰巧在 .htaccess 中使用了这个:
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
无论请求什么,这本质上都会调用 index.php.然后在您的 PHP 代码中,您可以查看 $_SERVER['REQUEST_URI'] 以获取 URL 并相应地对其进行解析.
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
RewriteCond 行用于排除对文件的直接调用.就我而言,出于性能原因,我不希望诸如对 js/css/image 文件的请求之类的内容通过 index.php.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
这篇关于url重写index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:url重写index.php
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01