如何允许由.htaccess中的URL三个可选参数?可选、参数、htaccess、URL

2023-09-02 10:03:42 作者:猫咕ぅ

我有 http://example.com 并检查是否某些URL存在PHP路由类。 我想打一个新的途径,那就是:

I have http://example.com and a PHP routing class that checks if some URL exists. I want to make a new route, which is:

http://example.com/foo/bar/123

但只要我打开它,Apache的重定向我到一个错误页面。所以我使用的.htaccess。在code是:

but as long as I open it, the Apache redirects me to an error page. So I'm using a .htaccess. The code is:


RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)  /index.php [L]

和它的工作原理,只要我使用 http://example.com/foo ,但一旦我添加一些其他的参数,它重定向我的错误。

and it works, as long as I use http://example.com/foo, but once I add some other parameters, it redirects me to an error.

我猜测,改写code是错误的。是不是错了? 如果是的话,你能建议我好吗? 如果没有,问题出在哪里可以找到?

I'm guessing that the rewrite code is wrong. Is it wrong? If yes, could you suggest me the good one? If no, where the problem could be located?

推荐答案

您可以处理URI与PHP的:

You can handle the URI with php:

的.htaccess:

.htaccess:

RewriteEngine on

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

PHP的:

php:

if (isset($_SERVER['REQUEST_URI']))
{
    $params = explode("/", ltrim($_SERVER['REQUEST_URI'], "/"));
    print_r($params);
}

example.com/just/these/params

输出:

Array
(
    [0] => just
    [1] => these
    [2] => params
)
 
精彩推荐
图片推荐