0

我试图弄清楚如何修改 .htaccess 文件,以便我可以做两件事:

  1. 不必在我的 PHP 文件中包含 .php 扩展名(例如,my.domain.com/page映射到的请求my.domain.com/page.php)。
  2. 做 #1 同时还包括额外的路径信息(例如,my.domain.com/page/path/stuff/here映射到的请求my.domain.com/page.php/path/stuff/here)。

通过将以下内容添加到 .htaccess 文件中,我发现了如何执行 #1:

# Allow PHP files without ".php" extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /$1.php [L,QSA]

但是,现在我想修改 RewriteRule 使其适用于#2。

4

3 回答 3

1

好的,在搜索 MultiViews 之后,我发现了几篇警告他们的文章(嗯,每个人都有自己的),但这也让我得到了一个使用 2 条规则而不是 1 条规则的答案:

RewriteRule ^([^\.]+)$ /$1.php [L]
RewriteRule ^([^\./]+)/(.*) /$1.php/$2 [L]

第一条规则捕获上面的案例#1,第二条规则捕获上面的案例#2。瞧!

于 2014-09-23T21:48:40.567 回答
0

您可以尝试使用Multiviews,它正是为了做到这一点:

Options +Multiviews
于 2014-09-23T16:59:05.227 回答
0
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([^\.]+)$ $1.php [NC,L] #Remove the .php

虽然不确定你想要什么路径的东西。

根据您的评论进行编辑,我在 php/angular 中使用了类似的东西。这可能不是“正确的”或最好的方法,但它对我有用。

访问

RewriteEngine       on
# Allow the API to function as a Front Controller
RewriteRule         ^api/(.*)$ api/index.php?rt=$1 [L,QSA,NC]
# Allow Angular to have Pretty URL's
RewriteCond         %{REQUEST_FILENAME} !-f
RewriteCond         %{REQUEST_FILENAME} !-d

api/index.php

// Pull the routing path
$router = explode('/', $_GET['rt']);

$version = $router[0];
$controller = $router[1];
$action = $router[2];

// Check for the file
if(file_exists($version . '/controllers/' . $controller .'.class.php')) {
    include $version . '/controllers/' . $controller .'.class.php';
} else {
    return false;
}

// Initialize and execute
$method = new $controller($action);
print $method->$action();

这让我可以在 url 中执行以下操作:api/v1/users/login,然后会在 V1 文件夹中找到 users.class.php 文件,并运行函数 login。

于 2014-09-23T17:02:32.467 回答