.htaccess - 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"
解决方案
请将您的 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]
推荐阅读
- php - Wordpress - 在函数文件中使用 Dompdf
- python - 当请求输入是元素列表时,蝗虫发布请求错误
- javascript - 在单行中对齐两个表
- javascript - 使用 Firebase 登录 Microsoft Teams 自定义选项卡
- r - R for lm 中的 autoplot():为什么我得到“恒定杠杆:残差与因子水平”而不是“残差与杠杆”图?
- c# - CollectionViewController 使用 c# 显示黑色背景,没有图像
- sql - ALL_TABLES、count(*) 和 count(PRIMARY_KEY) 有什么区别?
- sql - 如何获取与组中最新日期对应的值以填充 Oracle SQL 中的空值?
- flutter - Flutter localstorage.getItem() 返回 null
- python - “for”循环不继续(python)