动态URL重写.htaccess

时间:2012-04-26 12:27:18

标签: php .htaccess url-rewriting localhost

我是URlRewriting的新手,我在重写我的网址时遇到了一些问题,我有一个单独的.php索引页面,根据网址变量使用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

OR

RewriteRule ^(.*)/(.*)$ index.php?page=$1&content=$2

过去几天我一直在谷歌和Stackoverflow上四处寻找,但我似乎无法找到一个有效的解决方案,有没有人有任何想法如何让这个工作? 干杯

2 个答案:

答案 0 :(得分:2)

您只需要重写一次:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

然后在index.php文件中将路由拆分为piecies:

$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]`

保持控制器的概念 - > action-> do->重视其易于分配的路线。

?page=将是您的控制器

?content=将是您的操作

?id=将是您的子动作|做|价值等等

答案 1 :(得分:1)

尝试:

#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']变量以查看要求的内容。

这对你有用,让我知道你是怎么过的。

相关问题