我是URlRewrite的新手,我在重写URL时遇到了一些问题,我有一个单一的.php索引页面,其中的内容根据URL变量使用php填充。
我的问题是我似乎找不到正确的表达方式来让它工作。对于我的主页,我只发送 1 个变量,例如
"index.php?page=home"
但是对于其他页面,我最多使用 4 个变量,例如"index.php?page=about&content=about-news&id=27&pn=1"
现在,我可以使用以下方法让 1 或 2 个单独工作,但不一起工作:
RewriteRule ^((.*)+)$ index.php?page=$1
或
RewriteRule ^(.*)/(.*)$ index.php?page=$1&content=$2
在过去的几天里,我一直在谷歌和Stackoverflow上四处寻找,但我似乎找不到可行的解决方案,有没有人对如何让它工作有任何想法?干杯
尝试:
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
然后,您可以查看 $_SERVER['REQUEST_URI'] 变量以查看请求的内容。
这应该对你有用,让我知道你过得怎么样。
您所需要的只是一次重写:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
然后在索引中.php将路由拆分为馅饼:
$route = (!isset($_GET['route']))?'':$_GET['route']);
$parts = explode('/', $route);
So your old urls like this:
index.php?page=home
index.php?page=about&content=about-news&id=27&pn=1
index.php?page=$1
index.php?page=$1&content=$2
Become:
Example: `http://example.com/controller/action/do/value`
or `http://example.com/$parts[0]/$parts[1]/$parts[2]/$parts[3]/$parts[4]`
保持控制器>操作>执行>值的理念,分配路由很容易。
?page=
将成为您的控制者
?content=
将是你的行动
?id=
将是你的子操作 | do | value etc