【问题标题】:htaccess dynamic url redirecthtaccess 动态 url 重定向
【发布时间】:2021-06-30 01:33:48
【问题描述】:

我有以下网址

https://example.com/expert-profile?id=john-doe&locale=en

我希望它被重定向到

https://example.com/expert/john-doe

我尝试了以下

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^expert-profile$ https://example.com/expert/%3?%1%4 [L,R=301]

还有其他几个解决方案,这里没有任何效果。有人可以帮助我朝着正确的方向前进吗?

更新:

这是我当前的.htaccess 文件

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>

Redirect 301 "/en/download-app" "/download-app"

【问题讨论】:

  • 您发布的尝试到底发生了什么?它应该产生一个“有效”的重定向(尽管有一个额外的查询字符串)。如果什么都没有发生,那么您可能将指令放在错误的位置。

标签: .htaccess mod-rewrite


【解决方案1】:

请将您的 htaccess 文件保存在您的根目录中,并按以下方式保存。 请在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteCond %{QUERY_STRING} ^id=([^&]*)&locale=(.*)$ [NC]
RewriteRule ^([^-]*)-.*/?$ $1/%1-%2 [R=301,L]


如果您没有规则来处理不存在的文件/目录,请使用以下规则集。请确保使用上述规则或以下规则一次只设置一个。

RewriteEngine ON
RewriteBase /
RewriteRule ^index\.html$ - [L]

RewriteCond %{QUERY_STRING} ^id=([^&]*)&locale=(.*)$ [NC]
RewriteRule ^([^-]*)-.*/?$ $1/%1-%2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:expert)/([^-]*)-(.*)$ $1-profile?id=$1&locale=$2 [NC,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.html [L]

【讨论】:

  • 我试过了,还是不行。我已经用我当前的 htaccss 文件的内容更新了我的问题,我在哪里出错了?
  • @IbrahimAzharArmar,好的,您能否确认您在浏览器中点击了https://example.com/expert-profile?id=john-doe&amp;locale=en 链接?
  • 我确认,我点击了正确的 URL。我拥有的下载应用程序的另一条规则运行良好
  • @IbrahimAzharArmar,如果这是您点击的网址 https://example.com/expert-profile?id=john-doe&amp;locale=en,请尝试我的第二组规则,然后告诉我(我现在也在其中添加了您显示的规则),让我知道知道它是怎么回事。
  • 让我检查一下,一会儿再回来。谢谢。
【解决方案2】:

如果要重写,请捕获名称 (.+) 并将其插入目标 $1

RewriteRule ^expert/(.+)$ /expert-profile?id=$1&locale=en [L]

并且不要在这里使用标志R|redirect,除非你真的想要redirect.---


要从 expert-profile?id=john-doe 重定向到 expert/john-doe,请从查询字符串中捕获 id (.+?) 并将其插入替换 URL %1

RewriteCond &%{QUERY_STRING}& &id=(.+?)&
RewriteRule ^expert-profile$ /expert/%1 [R,L]

当一切正常时,您可以将R 替换为R=301 (permanent redirect)。

不要同时使用这两个规则。如果这样做,将导致无限的重定向循环并最终给出“500 Internal Server Error”。


不相关,但从不R=301测试!

【讨论】:

  • @IbrahimAzharArmar 您在浏览器中输入的 URL 是什么?是https://example.com/expert/john-doe,还是https://example.com/expert-profile?id=john-doe&amp;locale=en
【解决方案3】:

我有以下网址

https://example.com/expert-profile?id=john-doe&locale=en

我希望它被重定向到

https://example.com/expert/john-doe

您需要在.htaccess 文件的顶部您现有的指令之前执行以下操作(顺序很重要):

RewriteCond %{QUERY_STRING} (?:^|&)id=([^&]+)
RewriteRule ^expert-profile$ /expert/%1 [QSD,R=301,L]

这将捕获id URL 参数的值(在%1 反向引用中),无论它出现在查询字符串中的什么位置,并丢弃所有其他URL 参数。我假设您不需要特别匹配 locale=en

请注意,正则表达式子模式 ([^&amp;]+)id )仅匹配 something,而不匹配 nothing。如果 URL 参数为空(即id=&amp;locale=en),则不会发生重定向。

QSD 标志是丢弃原始查询字符串所必需的。

首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。并在测试前清除浏览器缓存。仅在确实打算永久时使用 301(永久)重定向。

将特定的URL/expert-profile?id=&lt;name&gt;&amp;locale=en重定向到/expert/&lt;name&gt;,即。 id 参数位于查询字符串的开头,然后是locale=en,只有这样您才能(并且应该)在条件中更具体。例如:

RewriteCond %{QUERY_STRING} ^id=([^&]+)&locale=en$
RewriteRule ^expert-profile$ /expert/%1 [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^expert-profile$ https://example.com/expert/%3?%1%4 [L,R=301]

这很接近(假设您将规则放在文件的顶部),但是,这会尝试保留其他 URL 参数,即。 locale=en 和其他任何东西,创建另一个查询字符串 - 您没有在您的要求中说明。


旁白:现有的答案是假设您想要内部重写(URL重写)另一个方向的请求,即。从/expert/john-doe/expert-profile?id=john-doe&amp;locale=en。这可能是由于这种性质的问题被臭名昭著地写错了,而这通常是真正的潜在意图。但是,您在这里没有提到这一点,/expert-profile 形式的 URL 不是有效的端点 - 所以在那个方向“重写” URL 真的没有意义。 (?)

【讨论】:

    猜你喜欢
    • 2015-10-01
    • 2016-09-18
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多