【问题标题】:I want to redirect each, old URLs to its corresponding new rewritten URLs using htaccess我想使用 htaccess 将每个旧 URL 重定向到其相应的新重写 URL
【发布时间】:2016-01-24 11:49:02
【问题描述】:

经过大量研究,我得出的结论是,我被困住了,需要帮助。这是我的 htaccess:

ErrorDocument 404 http://website.com/404.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule . http://%1%{REQUEST_URI} [R=301,L]
RewriteRule contact contact.php [NC,L]
RewriteRule about display.php?id=2 [NC,L]
RewriteRule services display.php?id=4 [NC,L]
RewriteRule rss.xml?$ rss.php [L]
</IfModule>

这成功地为每个 display.php?id= 查询创建了一个重写的 URL。现在我有两个 URL,如何将display.php?id=2 永久重定向到about

非常感谢您的帮助!

【问题讨论】:

  • 听起来你在问如何才能做与你刚刚做的完全相反的事情。也许尝试重新考虑您的问题并以更直接和直接的方式陈述它?我完全不知道您在这里寻求帮助的内容。
  • 嘿@Sherif!我已经成功地重写了一个旧的 URL,以阐明 URL 的确切位置。但是,我现在有两条路径:旧查询路径和新路径;我可以成功地走每条路。我如何 301 将旧查询路径重定向到重写路径,所以如果您在地址栏中键入旧路径,它将重定向到新路径,并在地址栏中显示新路径?我希望这会有所帮助!
  • 您在RewriteRule . http://%1%{REQUEST_URI} [R=301,L] 上方的重写规则之一中已经有一个 301 重定向。当您已经这样做时,您如何不知道如何创建这样的重定向,这让我感到迷惑。或许再看看Apache httpd docs 关于如何使用重写规则可能会刷新你的记忆?或者可能只是复制/粘贴您已经用于执行 301 并更改源/目标参数的重写规则?
  • 啊,我以为重定向是将所有www重定向到非www。我是否必须为重写的 URL 创建其他重定向规则?
  • 是的,但它是 301 重定向的示例。你是不明白自己的规则,还是随便从网上抄来的?

标签: php apache .htaccess redirect


【解决方案1】:

首先,清除浏览器缓存。我们不希望任何您已经尝试与我们的新逻辑混淆的缓存永久重定向:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#Use 302 redirects for debugging (the default if no redirect code is provided):
RewriteRule ^/?contact/?$ contact.php [NC,L,R]
RewriteRule ^/?about/?$ display.php?id=2 [NC,L,R]
RewriteRule ^/?services/?$ display.php?id=4 [NC,L,R]
RewriteRule ^/?rss.xml/?$ rss.php [NC,L,R]

如果您使用的是谷歌浏览器,您可以按 ctrl+shift+j 打开开发者工具。然后单击“网络”选项卡并选中“禁用缓存”复选框。然后再次尝试您的请求,看看它是否有效。注意我的代码使用 302 重定向,这是调试的最佳实践。一旦您可以验证它是否按预期工作,您可以将 302 重定向更改为 301 重定向以进行生产。

编辑:

要以其他方式重定向所有内容,请改用这个:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#Use 302 redirects for debugging (the default if no redirect code is provided):
RewriteRule ^/?contact.php/?$ contact [NC,L,R]
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R]
RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R]
RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R]

编辑2:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#handle silent rewrites first:
RewriteCond %{THE_REQUEST} ^\w+\s+/?about
RewriteRule ^/?about/?$ /display.php?id=2&r=%1 [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?contact
RewriteRule ^/?contact/?$ contact.php [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?services
RewriteRule ^/?services/?$ display.php?id=4 [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?rss\.xml
RewriteRule ^/?rss.xml/?$ rss.php [NC,L]


#handle redirects:
RewriteCond %{THE_REQUEST} ^\w+\s+/?display.php
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?contact\.php
RewriteRule ^/?contact.php/?$ contact [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?display\.php
RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?rss\.php
RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R]

编辑3: 实际上,我们可以在这里使用END 标志来获得更简洁的代码:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#handle silent rewrites first:
RewriteRule ^/?about/?$ /display.php?id=2&r=%1 [NC,L,NS,END]
RewriteRule ^/?contact/?$ contact.php [NC,L,NS,END]
RewriteRule ^/?services/?$ display.php?id=4 [NC,L,NS,END]
RewriteRule ^/?rss.xml/?$ rss.php [NC,L,NS,END]

#handle redirects:
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R,END]

RewriteRule ^/?contact.php/?$ contact [NC,L,R,END]

RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R,END]

RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R,END]

【讨论】:

  • 你的重定向是错误的:“我怎么能...重定向display.php?id=2about
  • 这部分:“这成功地为每个 display.php?id= 查询创建了一个重写的 URL。”验证了用户对重写的理解是倒退的,因为他们正在解释他们发布的 .htaccess 的当前行为。通常,您希望左侧是干净的 URL,右侧是 PHP 文件。 stackoverflow 上的许多人对重写工作的方向感到困惑。所以我相应地回答了。
  • 感谢@Ultimater 的帮助!但是,w3d 是对的,我想将 display.php?id=2 重定向到 about。目前,我看到上述逻辑重定向到 display.php?id=2。我为混乱/误解道歉;看来我的问题描述性不够。
  • @junjun 已编辑。如果可行,请将临时 302 重定向 R 标志更改为永久重定向 R=301 以便稍后进行生产,但继续使用 302 重定向进行开发。
  • @Ultimater 嗯,不幸的是它不起作用。我试着玩弄它并试图理解它。但是,由于未创建重写的 URL,重定向不会起作用吗?我想也许先创建重写的 URL,然后尝试将旧 URL 重定向到新 URL 会起作用,但它不起作用。
猜你喜欢
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 2022-06-11
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多