1

我有一个FrontController期待两个$_GET参数:

controller
action

对该站点的典型调用如下所示:

http://foo.bar/index.php?controller=start&action=register

我想要做的是允许用户通过以下网址访问该网站:

http://foo.bar/start/register

我试过的:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)/(.+)$ index.php?controller=$1&action=$2 [L,QSA]
</IfModule>

由于这给了我 404 错误,它似乎不起作用。

mod_rewrite本身在服务器上启用。

4

2 回答 2

2

你为我发布的.htaccess作品:

// GET /cont1/action1

print_r($_GET);

/* output
Array
(
    [controller] => cont1
    [action] => action1
)
*/

您可能想尝试使用绝对路径index.php而不是相对路径。

无论如何,该正则表达式将导致:

// GET /cont1/action1/arg1

print_r($_GET);

/* output
Array
(
    [controller] => cont1/action1
    [action] => arg1
)
*/

你最好这样做:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

并让你index.php分成$_GET['url']控制器,动作,参数等......

于 2012-05-16T19:35:54.127 回答
0

让这个工作有两个部分。如果您正在使用 PHP 和 Apache,那么您的服务器上必须有可用的重写引擎。

在您的用户文件夹中放置一个以.htaccess这些内容命名的文件:

 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule . index.php [L]

然后index.php你可以使用REQUEST_URIserver 变量来查看请求的内容:

<?php
$path = ltrim($_SERVER['REQUEST_URI'], '/'); 
echo $path;
?>

如果有人要求/start/register,那么假设所有上述代码都在 html 根目录中,$path变量将包含start/register.

我将使用explode 函数$path作为/分隔符并将第一个元素拉为寄存器。

重写代码具有使用文件名和目录名的好处。

于 2016-01-05T18:02:37.323 回答