简体   繁体   English

使用mod_rewrite和apache进行“动态”重写

[英]“Dynamic” rewrite with mod_rewrite and apache

I´ve a bit of a "strategic" problem with creating a nice mod_rewrite solution for a shop. 在为商店创建一个不错的mod_rewrite解决方案时,我遇到了一个“战略”问题。 The shop has different kind of pages : 该商店的页面种类不同

  • Products (products.php) 产品(products.php)
  • Categories (category.php) 类别(category.php)
  • Content (content.php) 内容(content.php)

So the "default" URI´s look like this : 因此, “默认” URI如下所示

  • /products.php?id=2 /products.php?id=2
  • /category.php?id=4 /category.php?id=4
  • /content.php?id=5 /content.php?id=5

The final result should be a URI´s that include the name of the page (product name, category name). 最终结果应该是包含页面名称(产品名称,类别名称)的URI。

  • /chocholate-icecream -> product.php?id=2 / chocholate-icecream-> product.php?id = 2
  • /coffe-and-ice -> category.php?id=4 / coffe-and-ice-> category.php?id = 4
  • /imprint -> content.php?id=5 /版本记录-> content.php?id = 5

My problem now is, that I´ve different types of content which maps to different .php files . 我现在的问题是, 我有不同类型的内容 ,它们映射到不同的.php文件 And I´m unable to determine to which .php file to route with mod_rewrite as long as I don´t use a cheat "like **/product/**chocolate-icrecream" so that I know that this URI is made for a product. 而且,我无法确定要使用mod_rewrite路由到哪个.php文件,只要我不使用作弊行为(例如“ ** / product / ** chocolate-icrecream”),这样我就知道此URI是为产品。 But me and my customer don´t want that solution. 但是我和我的客户不想要那种解决方案。

I´ve already started another try: 我已经开始另一次尝试:

  • ALL requests which don´t match a physical file or folder are routed to a mapper.php file. 与物理文件或文件夹不匹配的所有请求将路由到mapper.php文件。 This file looks in the database - I´ve an index with all products, categories etc. - and gives me the correct file. 该文件在数据库中查找-我拥有所有产品,类别等的索引-并为我提供了正确的文件。
  • The mapper then loads the content via CURL and shows it to the user 然后, 映射器通过CURL加载内容并将其显示给用户

Basically I thought this was a nice idea but there are so many problems with session handling, , post data, php based redirects (header: ...) with CURL that I really plan to cancel this way etc. etc. 基本上我认为这是一个好主意,但是会话处理,发布数据,基于PHP的CURL重定向(标头:...)有很多问题,我确实打算取消这种方式,等等。

So my question is: Has anyone of you an idea or a pattern that helps me creating the "good looking" URI´s without using a complex CURL mapper? 所以我的问题是:你们中的任何人有没有一个想法或模式可以帮助我在不使用复杂的CURL映射器的情况下创建“好看的” URI?

Thanks a lot in advance, Mike 谢谢你,迈克

UPDATE to "Include solution": 更新为“包含解决方案”:

A good example for the "include problem": A request for www.shop.com/cart is "rewritten" to "mapper.php". “包含问题”的一个很好的例子:将www.shop.com/cart的请求“重写”为“ mapper.php”。 The "mapper.php" decides to include www.shop.com/shoppingcart.php. “ mapper.php”决定包含www.shop.com/shoppingcart.php。

shoppingcart.php uses smarty to display the cart. shoppingcart.php使用smarty来显示购物车。 The view asks the $_SERVER variable, if the actual called file is "shoppingcart.php" to decide if the page should be shown in fullscreen mode or with additional columns. 该视图询问$ _SERVER变量,是否实际的调用文件是“ shoppingcart.php”,以确定该页面是应以全屏模式显示还是带有其他列。

In the "normal / direct" call $_SERVER['PHP_SELF'] would be "/shopping_cart.php" which will show the fullscreen version. 在“普通/直接”调用中,$ _ SERVER ['PHP_SELF']将是“ /shopping_cart.php”,它将显示全屏版本。 Going over the mapper.php with the include option would give us $_SERVER['PHP_SELF']=='/mapper.php' which will give us the view with columns (what´s wrong). 使用include选项遍历mapper.php将给我们$ _SERVER ['PHP_SELF'] =='/ mapper.php'这将为我们提供带有列的视图(错了)。

You could do this simply by pointing them all to one page and then including the proper script based on the slug. 您可以简单地通过将它们全部指向一页然后包含基于段的适当脚本来执行此操作。 For example, rewrite all requests like index.php?slug=chocolate-icrecream. 例如,重写所有请求,例如index.php?slug = chocolate-icrecream。

index.php index.php

$resource = fetch_resource($slug);
if ($resource->slug == 'product') 
{
  include('products.php');
}
else if ($resource->type == 'category')
{
  include('category.php');
}
else
{
  include('content.php');
}

I don't understand why you don't want to structure your URLs like /product/chocolate-icrecream. 我不明白为什么您不希望构建/ product / chocolate-icrecream之类的URL。 That would be good semantic naming that is fairly consistent across the internet nowadays. 那将是一个很好的语义命名,它在当今的整个互联网上是相当一致的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM