Multiple query parameter RESTful URL with .htaccess

风格不统一 提交于 2019-12-08 03:02:18

问题


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

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

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

Oscar


回答1:


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>

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...

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

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

Hope this helps.

-Mike



来源:https://stackoverflow.com/questions/3475355/multiple-query-parameter-restful-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!