多个查询参数的RESTful URL以的.htaccess [英] Multiple query parameter RESTful URL with .htaccess

查看:101
本文介绍了多个查询参数的RESTful URL以的.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的REST风格的URL网站我的工作。

I would like to use following RESTful URL for a web site I'm working on.

http://mysite.com/Products/category=bags&colours=black

谁能告诉我怎么用的.htaccess来实现这一目标?

can anyone please tell me how to achieve this with .htaccess?

奥斯卡

推荐答案

下面是一个.htaccess捕捉到任何东西,除了当文件或目录冲突,其路由到的index.php:

Here's an .htaccess to capture anything except when files or directories conflict and it routes to /index.php:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

使用,我们可以使用的index.php作为我们的请求路由器。在PHP中,我们简单地获得请求的东西像这样:

With that we can use /index.php as our request router. In PHP we simply get the request something like so:

<?php
$url_path = $_SERVER['REQUEST_URI'];
// Parse your URL path here...

这是说,你的URL模式是非标准的,我真想建议反对,因为代理服务器和浏览器将URL连接$ C C&符号和等号$。我建议下列操作之一,而不是(也使用小写的,因为它的URL验证帮助):

That said, your URL pattern is non-standard and I'd really recommend against it because proxies and browsers will URL encode the ampersand and equal signs. Let me suggest one of the following instead (and also to use lowercase as it helps with validation of the URL):

http://example.com/products/categories/bags/colours/black
http://example.com/products/category_bags/colours_black
http://example.com/products?category=bags&colours=black
http://example.com/products/categories/bags?colours=black
http://example.com/products/bags?colours=black

要看看有什么会连接code在URL只是谷歌它的路径,看看谷歌的搜索URL。

To see what will encode in the path of a URL just Google it and look at Google's search URL.

希望这有助于。

-Mike

这篇关于多个查询参数的RESTful URL以的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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