如何在laravel中用大写或小写创建URL?
对于SEO,我需要创建大写或小写形式的URL 例如(原始URL): 如果用户运行此URL:如何在laravel中用大写或小写创建URL?,laravel,laravel-5,routes,Laravel,Laravel 5,Routes,对于SEO,我需要创建大写或小写形式的URL 例如(原始URL): 如果用户运行此URL: https://example.com/category/product or https://example.com/CATEGORY/PRODUCT 转换为: https://example.com/CATEGORY/product 如何在web.php中创建此URL?类似的方法会奏效(我正在假设您使用的是什么类别和产品参数) Route::get('{category}/{product}',
https://example.com/category/product or https://example.com/CATEGORY/PRODUCT
转换为:
https://example.com/CATEGORY/product
如何在
web.php
中创建此URL?类似的方法会奏效(我正在假设您使用的是什么类别和产品参数)
Route::get('{category}/{product}', function($category, $product)
{
$product = Product::where('product_title', strtolower($product))
->and('category', strtolower($category))
->get();
return $product;
});
要点:
- 路由器不在乎它的大小写
- 您需要确定如何从一个案例转换为另一个案例-在上面的示例中,我只是转换为小写(但您可能需要使用studley案例、烤肉串案例等(查看文档的Laravel helpers部分))
Route::get('{category}/{product}', function($category, $product)
{
if(strtoupper($category) != $category || strtolower($product) != $product) {
return redirect(strtoupper($category).'/'.strtolower($product));
}
$product = Product::where('product_title', strtolower($product))
->and('category', strtolower($category))
->get();
return $product;
});
默认情况下,Laravel返回302代码,表示临时重定向。这是因为意外发送临时重定向要比意外发送永久重定向好得多。在这种情况下,听起来您可能希望将其永久化,因此将返回代码添加到重定向调用中
return redirect(strtoupper($category).'/'.strtolower($product), 301);
这样,他们的浏览器就会记住你想要的路线。它还会指示搜索引擎索引正确的url
与Apache不同,NGNXURL是区分大小写的。这是为了提高性能。LARAVER路由也是区分大小写的。因此,如果您使用LaaLeV+nGixx堆栈,则必须考虑所有路由中的情况。您可以处理NGIX配置中的重定向,但在这种情况下,匹配/可以很容易地捕获您无意的URL。p> 你为什么需要它?URL基本上是大小写不变的。@EduardMalakhov,嗨,搜索引擎优化的双重URL错误。我只需要运行这个URL:
https://example.com/CATEGORY/product
URL不区分大小写。你所说的双重URL是什么意思?你可以共享更多信息。向我们展示你现有的路由(包括一个不工作的路由示例)
return redirect(strtoupper($category).'/'.strtolower($product), 301);