首页 > 解决方案 > htaccess 动态 url 重定向

问题描述

我有以下网址

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"

标签: .htaccessmod-rewrite

解决方案


请将您的 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]

推荐阅读