【问题标题】:Redirect Urls With Parameters Using Htaccess使用 Htaccess 重定向带有参数的 URL
【发布时间】:2015-09-10 21:16:48
【问题描述】:

我在 google 中做了一个“site:”命令来查看索引的 url,发现很多带有参数的 url 都指向同一个页面。

它是一个静态站点,但大约 5 年前它是一个 Wordpress 站点,这可能是 Google 创建这些链接并为其编制索引的方式。我找到的3个主要链接如下。它们看起来都像这 3 个,但 id 不同。

http://example.com/index.php?option=com_banners&task=click&bid=41

http://example.com/?option=com_content&view=article&id=86&Itemid=201

http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7

我的问题是如何将这些带有所有不同参数 ID 的链接重定向到它们对应的页面。前两个进入主页,第三个进入 /phlebotomy-jobs 页面。

所以,我正在寻找的是,例如,这个网址:http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7 重定向到http://example.com/phlebotomy-jobs

基本上从 ?开。

【问题讨论】:

  • 你使用任何类型的框架吗?
  • 不,我不使用框架。从头开始构建。 90% 的 html 加上一点 php 和 jquery。

标签: php .htaccess redirect


【解决方案1】:

好吧,不确定这是否是“最好”的方式,但例如,这可行:

RewriteCond %{QUERY_STRING} option=
RewriteRule (.*) /$1? [R=301,L]

【讨论】:

    【解决方案2】:
    RewriteEngine On
    RewriteRule ^index.php?$ /path/page.php [R=301,L,QSA]
    RewriteRule ^$ /path/page.php [R=301,L,QSA]
    RewriteRule ^phlebotomy-jobs?$ /path/page.php [R=301,L,QSA]
    

    QSA RewriteRule 标志附加查询字符串。

    https://httpd.apache.org/docs/2.4/rewrite/flags.html

    编辑:

    由于您不关心保留查询字符串:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} (^/index.php$|^/$|^/phlebotomy-jobs$)
    RewriteCond %{QUERY_STRING} .
    RewriteRule (.*) /$1? [R=301,L]
    

    注意:此方法可防止将 any 查询字符串传递到这两个页面中的任何一个。

    【讨论】:

    • 嗯,这似乎并没有从 ?上。它只是在问号之前添加到网址。也许我没有正确实施或不完全清楚。例如,这个 url:example.com/… 应该重定向到 example.com/phlebotomy-jobs
    猜你喜欢
    • 2022-07-01
    • 1970-01-01
    • 2022-12-29
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    相关资源
    最近更新 更多