简体   繁体   English

如何从像“example.com/products/123/title-of-this-product”这样干净的 url $_GET['id']?

[英]How to $_GET['id'] from a clean url like "example.com/products/123/title-of-this-product"?

I'd like my URL to look like this:我希望我的 URL 看起来像这样:

example.com/products/123/title-of-this-product

The actual URL is this:实际的 URL 是这样的:

example.com/products.php?id=123&title=title-of-this-product

The .htaccess file correctly interprets my URLs so that users who are on this page only see the clean URL. However, if I try to use $_GET['id'] on the products.php page, the script crashes because it doesn't recognize any id in the URL. .htaccess文件正确解释了我的 URL,因此该页面上的用户只能看到干净的 URL。但是,如果我尝试在products.php页面上使用$_GET['id'] ,脚本会崩溃,因为它不会识别 URL 中的任何id

.htaccess code: .htaccess代码:

Options +FollowSymLinks +MultiViews

RewriteEngine On
RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]

products.php code: products.php代码:

$product_id = isset($_GET['id']) ? $_GET['id'] : "Error! I can't find your product!";

How can I retain the URL parameters for PHP functions if I want a clean URL?如果我想要一个干净的 URL,如何保留 PHP 函数的 URL 参数?

You actually have 2 problems here...你实际上在这里有2个问题......

  1. MultiViews (part of mod_negotiation) is enabled and it's this that is serving products.php . MultiViews (mod_negotiation 的一部分)已启用,它正在为products.php提供服务。
  2. Your RewriteRule pattern is incorrect and won't match the requested URL.您的RewriteRule模式不正确,与请求的 URL 不匹配。
 Options +FollowSymLinks +MultiViews

You need to disable MultiViews (you have explicitly enabled it).您需要禁用MultiViews (您已明确启用它)。 It is MultiViews (part of mod_negotiation) that is serving your products.php file (without any URL parameters), not the mod_rewrite directive that follows.为您的products.php文件(没有任何 URL 参数)提供服务的是 MultiViews(mod_negotiation 的一部分),而不是后面的 mod_rewrite 指令。 MultiViews essentially allows extensionless URLs with minimal effort, however, it can be the cause of unexpected conflicts (with mod_rewrite) - as in this case. MultiViews 本质上允许以最小的努力使用无扩展名的 URL,但是,它可能是意外冲突的原因(使用 mod_rewrite)——就像在这种情况下一样。

Your RewriteRule directive is not actually doing anything.您的RewriteRule指令实际上没有做任何事情。 If the .htaccess file is located in the document root then the RewriteRule pattern ^([0-9]+)(?:/([^/]*))?/?$ does not match the requested URL ( /products/123/title-of-this-product ), so the directive is not actually processed at all (although MultiViews would still override this even if it did).如果.htaccess文件位于文档根目录中,则RewriteRule模式^([0-9]+)(?:/([^/]*))?/?$与请求的 URL ( /products/123/title-of-this-product ),所以该指令实际上根本没有被处理(尽管 MultiViews 仍然会覆盖它,即使它被处理了)。

Try it like this instead:试试这样:

# Disable MultiViews
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteRule ^products/([0-9]+)(?:/([^/]*))?/?$ products.php?id=$1&title=$2 [L,NC]

You were missing products from the start of the URL-path matched by the RewriteRule pattern .您从与RewriteRule pattern匹配的 URL 路径的开头缺少products Without products/ at the start of the regex it would only match if you are in a /products/ subdirectory, ie.如果没有products/在正则表达式的开头,它只会在您位于/products/子目录中时匹配,即。 /products/.htaccess . /products/.htaccess The RewriteRule directive matches relative to the location of the .htaccess file itself. RewriteRule指令与.htaccess文件本身的位置相匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 URL重写-如何将example.com/pageid.php?id=12转换为example.com/full-title - URL rewriting- How can I turn example.com/pageid.php?id=12 to example.com/full-title 如何处理从example.com/search/?s=some到example.com/search/some的URL - How to manipulate the URL from example.com/search/?s=some to example.com/search/some 像groovehark / twitter这样友好的网址:example.com/#!/whatever - Friendly URL like grooveshark/twitter: example.com/#!/whatever 如何将example.com/index.php#anchor的URL重写为example.com/anchor? - How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor? 如何使用PHP将主机example.com与诸如user@example.com之类的电子邮件地址分开? - How I separate the host example.com from an e-mail address like user@example.com using PHP? Apache 2阻止网址,例如example.com/?admin - Apache 2 block url such as example.com/?admin 如何从网址中获取PHP输入参数,例如www.domain.com/item/ABC123 - How to take PHP input parameter from URL like this www.domain.com/item/ABC123 如何从标题为example.com的页面传递表单?a = 100? - How to pass form from a page with header as example.com?a=100? 在像 example.com/wordpress 这样的 url 上设置 wodpress 的配置是什么? - What is the configuration for setting up wodpress on a url like example.com/wordpress? 使用,htaccess将example.com?id=12重定向到example.com/index.php?id=12 - using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM