如何通过 .htaccess 在 URL 中允许三个可选参数? [英] How to allow three optional parameters in the URL by .htaccess?

查看:19
本文介绍了如何通过 .htaccess 在 URL 中允许三个可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 http://example.com 和一个 PHP 路由类,用于检查某个 URL 是否存在.
我想开辟一条新路线,即:

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

但是只要我打开它,Apache 就会将我重定向到错误页面.所以我使用.htaccess.代码是:

<前>重写引擎开启RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^(.*)/index.php [L]

它可以工作,只要我使用 http://example.com/foo,但是一旦我添加了一些其他参数,它就会将我重定向到一个错误.

我猜重写代码是错误的.这是错的吗?
如果是的话,你能给我推荐一个好的吗?
如果不是,问题可能出在哪里?

解决方案

您可以使用 php 处理 URI:

.htaccess:

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^ index.php [L]

php:

if (isset($_SERVER['REQUEST_URI'])){$params = expand("/", ltrim($_SERVER['REQUEST_URI'], "/"));打印_r($params);}

example.com/just/these/params

输出:

数组([0] =>只是[1] =>这些[2] =>参数)

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

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]

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.

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?

解决方案

You can handle the URI with php:

.htaccess:

RewriteEngine on

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

php:

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

example.com/just/these/params

Output:

Array
(
    [0] => just
    [1] => these
    [2] => params
)

这篇关于如何通过 .htaccess 在 URL 中允许三个可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆