动态URL重写的.htaccess(dynamic URL rewriting .htaccess)

2019-09-16 19:22发布

我是新来的URL重写,我一直有一些问题,重写我的网址,我有其中的内容取决于URL的变量使用PHP填充一个单一.PHP索引页。

我的问题是,我似乎无法找到正确的表达得到它的工作。 对于我的主页,我只发出了像1个变量

"index.php?page=home"

但对于其他的网页我用了4个变量,如“的index.php?页=约含量约-新闻&ID = 27&PN = 1”

现在,我得到尽可能获得1或2使用seperately但不能一起工作:

RewriteRule ^((.*)+)$ index.php?page=$1

要么

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

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

Answer 1:

所有你需要的是一个改写:

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

保持到控制器 - >动作 - >做 - >想法看重它很容易分配路线。

?page=将是你的控制器

?content=将是你的行动

?id=将是你的子操作| 做| ECT



Answer 2:

尝试:

#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”]变量来看看有什么要求。

这应该为你工作,让我知道你上车。



文章来源: dynamic URL rewriting .htaccess