.htaccess htaccess动态url重定向

.htaccess htaccess动态url重定向,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有以下网址 https://example.com/expert-profile?id=john-doe&locale=en https://example.com/expert-profile?id=john-doe&locale=en 我想把它重定向到 https://example.com/expert/john-doe https://example.com/expert/john-doe 我尝试了以下方法 RewriteCond %{QUERY_STRING}

我有以下网址

https://example.com/expert-profile?id=john-doe&locale=en
https://example.com/expert-profile?id=john-doe&locale=en
我想把它重定向到

https://example.com/expert/john-doe
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"

重新启动发动机
重写基/
重写规则^index\.html$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-L
重写规则/index.html[L]
重定向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]

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

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

RewriteRule ^expert/(.+)$ /expert-profile?id=$1&locale=en [L]
不要在这里用旗子,除非你真的想用旗子---


要从
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
()

不要同时使用这两条规则。如果您这样做,它将导致无休止的重定向循环,并最终给出“500内部服务器错误”



无关,但从不测试

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

RewriteRule ^expert/(.+)$ /expert-profile?id=$1&locale=en [L]
不要在这里用旗子,除非你真的想用旗子---


要从
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
()

不要同时使用这两条规则。如果您这样做,它将导致无休止的重定向循环,并最终给出“500内部服务器错误”


无关,但从不测试

我有以下网址

https://example.com/expert-profile?id=john-doe&locale=en
https://example.com/expert-profile?id=john-doe&locale=en
我想把它重定向到

https://example.com/expert/john-doe
https://example.com/expert/john-doe
在现有指令之前,您需要在
.htaccess
文件的顶部执行以下操作(顺序很重要):

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

请注意,regex子模式
([^&]+)
id
值)只匹配某些内容,而不是任何内容。如果URL参数为空(即id=&locale=en),则不会发生重定向

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

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

要将特定URL
/expert profile?id=&locale=en
重定向到
/expert/
,即
id
参数位于查询字符串的开头,后跟
locale=en
,只有这样,您才能(而且应该)在条件下更具体。例如:

RewriteCond %{QUERY_STRING} ^id=([^&]+)&locale=en$
RewriteRule ^expert-profile$ /expert/%1 [QSD,R=301,L]
这很接近(如果您将规则放在文件的顶部),但是,这会尝试保留其他URL参数,例如,
locale=en
和其他任何参数,以创建另一个查询字符串-您在需求中没有说明这一点


旁白:现有答案假设您希望在另一个方向内部重写(URL重写)请求,即从
/expert/john doe
/expert profile?id=john doe&locale=en
。这可能是由于这种性质的问题是如何被众所周知地写错的,而这往往是真正的潜在意图。但是,您在这里没有提到这一点,并且
/expert profile
形式的URL不是有效的端点-因此,在该方向“重写”URL是没有意义的。(?)

我有以下网址

https://example.com/expert-profile?id=john-doe&locale=en
https://example.com/expert-profile?id=john-doe&locale=en
我想把它重定向到

https://example.com/expert/john-doe
https://example.com/expert/john-doe
在现有指令之前,您需要在
.htaccess
文件的顶部执行以下操作(顺序很重要):

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

请注意,regex子模式
([^&]+)
id
值)只匹配某些内容,而不是任何内容。如果URL参数为空(即id=&locale=en),则不会发生重定向

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

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

将特定URL
/expert profile?id=&locale=en
重定向到
/expert/
,即
id
参数位于查询字符串的开头,后跟
l