大网数据广告活动
网站运营与故障问答

How can I redirect my URL to rearrange the order of subdirectories in .htaccess?

· 52CE 我爱测

My current URL is example.com/example/{dynamic_id}/{dynamic_name}/static/1.html I want to redirect it to this, example.com/{dynamic_name}/example/{dynamic_id}/static/1.html I tried but failed.

问题原文

原帖链接 · Aatish Kumar · CC BY-SA 4.0

My current URL is

example.com/example/{dynamic_id}/{dynamic_name}/static/1.html

I want to redirect it to this,

example.com/{dynamic_name}/example/{dynamic_id}/static/1.html

I tried but failed.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Maybe something like:

RewriteEngine On

RewriteRule ^(example)/(\d+)/([\w-]+)/(static/1\.html)$ /$3/$1/$2/$4 [R=302,L]

This externally "redirects" a URL of the form /example/{dynamic_id}/{dynamic_name}/static/1.html to /{dynamic_name}/example/{dynamic_id}/static/1.html.

I'm assuming that {dynamic_id} consists of only digits and {dynamic_name} is alphanumeric and can include underscores (_) and hyphens (-).

$1, $2, $3 and $4 are backreferences to the corresponding captured groups in the RewriteRule pattern.

UPDATE: Specifically, the regular expression (regex) ^(example)/(\d+)/([\w-]+)/(static/1\.html)$ matches as follows:

  • ^ - asserts the start of the string. So whatever follows matches from the beginning of the URL-path.

  • (example) - matches the literal string "example" (at the start of the URL-path - note that the URL-path that is matched does not start with a slash). The surrounding parentheses ( ... ) make this a capturing group, which can later be referenced in the substitution string using $1 (the number indicates the position of the capturing group in the regex). Capturing this simply saves repetition later, as we can refer to this using $1, instead of having to repeat the word "example". This is a simple example but is good practice to save repetition of longer strings.

  • / - matches a literal slash.

  • (\d+) - matches 1 or more digits (ie. 0-9). \d is a shorthand character class that matches a digit 0-9. And the + quantifier matches 1 or more of these. Again, this is a capturing group, so is saved in the backreference $2.

  • ([\w-]+) - matches 1 or more word (or hyphen) characters. The \w is a shorthand character class indicating a word character, ie. a-z, A-Z, 0-9 or _. The hyphen (-) is added to the character class. This is another capturing group, so is saved in the backreference $3.

  • (static/1\.html) - matches the literal string "static/1.html". The dot (.) is a special meta-character that matches any character, so we need to backslash-escape the dot to match a literal dot. This is saved in the backreference $4.

  • $ - asserts the end of the string. So, the regex that occurs between the ^ (start) and $ (end) must match the entire URL-path.

can u solve this too, example.com/static/{dynamic_id}/{dynamic_name}.html to example.com/{dynamic_name}/static/{dynamic_id}.html

i tried this but did't work.
RewriteRule ^(example)/(\d+)/([\w-]+)$ /$3/$1/$2.html [R=302,L]

That is very close, but you are just missing the .html (specifically \.html in regex syntax) from the end of the last path segment, since you need to match {dynamic_name}.html, not just {dynamic_name}. (Without capturing the ".html" part.)

Consequently the above will fail to match and it just won't do anything (or it matches the wrong URL and redirects incorrectly).

So, you would need to change it slightly like this:

RewriteRule ^(static)/(\d+)/([\w-]+)\.html$ /$3/$1/$2.html [R=302,L]

The order of the two rules does not matter in this instance as there does not appear to be a conflict.

However, one thing to be aware of is redirect-loops. Where the URL you are redirecting "to" also matches a URL-pattern you are redirecting "from". But that looks OK here.

i want to redirect my old urls to new urls instead of 404

Note that the above are currently 302 (temporary) redirects. However, if you are redirectign "old" URLs then you probably want to change these to 301 (permanent) at some point. Always good to test with 302s to avoid potential caching issues.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-10-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。