PHP重定向后如何实现mod_重写?

PHP重定向后如何实现mod_重写?,php,mod-rewrite,url-redirection,Php,Mod Rewrite,Url Redirection,我有很多旧的网址,我需要重定向到某些类别 www.example.com/some request product->www.example.com/index.php?categories=一些请求的产品 www.example.com/some requested article->www.example.com/index.php?articles=一些请求的文章 www.example.com/some requested user->www.example.com/index.php

我有很多旧的网址,我需要重定向到某些类别

  • www.example.com/some request product->www.example.com/index.php?categories=一些请求的产品
  • www.example.com/some requested article->www.example.com/index.php?articles=一些请求的文章
  • www.example.com/some requested user->www.example.com/index.php?users=some requested user
等等,我希望你能明白这一点。(实际上,URL中没有关于内容类型的提示。)

我已经创建了重定向,现在我可以访问我需要的内容:

  • 重写规则^([a-zA-Z0-9-]+)$/index.php%{REQUEST_URI}
在PHP中,我现在可以查询URL以查看内容的类型:

  • if($\u SERVER['REQUEST\u URI']==“/some requested content”){Redirect($BaseURL./index.php?category\u type=some requested content”,false);}
进行重定向,获取内容。(我知道这不安全等,只是为了测试。)

我现在的主要问题是,在发生这些事件后,如何屏蔽URL?

我希望在浏览器的地址栏中看到最初请求的相同URL(www.example.com/some-requested-content),而不是它重定向到的URL

可能吗?
或者您可以考虑另一种解决方案吗?

您正在寻找查询字符串append()

依此类推,您可以让正则表达式覆盖查询字符串的每一种情况,比如类别、文章、用户等等

在重写规则中,L表示最后一个处理,这一点非常重要

这将保留可见的URI,但在内部将所需的查询字符串数据传递给处理请求的页面

因此,您并不是真正地“屏蔽”url,url基本上只是简单地填充所需的查询字符串数据,然后将其发送到处理请求的文件。这是一种常见的方法,例如,许多博客和cms平台就是这样处理搜索引擎友好的URL的

但是请注意,因为您没有在每种情况下提供更准确的真实URL示例,所以我发布的解决方案需要处理变量值的正则表达式。如果类型之间没有实际的模式差异,那么您就不能使用此方法,您必须查看每个查询字符串类型的精确值/URL列表,并使用RewriteCond测试中的[OR]选项将它们列为可能的匹配项。只要不涉及太多的页面,这就行了

像这样:

RewriteCond %{REQUEST_URI} ^/beans-are-fun$ [OR]
RewriteCond %{REQUEST_URI} ^/rice-is-fun$ [OR]
RewriteCond %{REQUEST_URI} ^/fun-with-rice-and-beans$
RewriteRule ^(.*)$ index.php?articles=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/beans-are-fun$ [OR]
RewriteCond %{REQUEST_URI} ^/rice-is-fun$ [OR]
RewriteCond %{REQUEST_URI} ^/fun-with-rice-and-beans$
RewriteRule ^(.*)$ index.php?articles=$1 [L,QSA]