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

htaccess rewrite to move numeric id from the front of the url to the end and remove the ".html"

· 52CE 我爱测

I'm having a problem with site migration and old links. Now, its not just a server migration, it was also a CMS migration. Site went from umbraco (asp.net) to wordpress. Problem is that google indexed old URLs. We have a way to r…

问题原文

原帖链接 · klo · CC BY-SA 3.0

I'm having a problem with site migration and old links. Now, its not just a server migration, it was also a CMS migration. Site went from umbraco (asp.net) to wordpress. Problem is that google indexed old URLs. We have a way to rework links to make them work. I've been using this method to rework links and used WP plugin for redirect. The problem is there is way to many links for this plugin.

I was wondering is there any way to rework links dynamically with htaccess. So, here is how links should be rewritten:

http://example.com/something/XXXXX-some-text.html
-->
http://example.com/something/some-text-XXXXX
where XXXXX is some number between 100 and 100,000

This method works, only problem is that there is more then 60,000 links...

Any suggestions?

P.S. There are also some other links that should be skipped, cause we don't have a way to rework them...

已采纳解答

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

http://example.com/something/XXXXX-some-text.html
-->
http://example.com/something/some-text-XXXXX
where XXXXX is some number between 100 and 100,000

I assume "100,000" doesn't literally have a comma in it? And I've assumed that the hyphen (-) after the number is literal, so it's number followed by a single hyphen, followed by <some-text>. And that <some-text> can only consist of upper/lowercase letters, numbers and hyphens. Or is it supposed to be XXXXX<sometext>.html?

You can try something like the following in your root .htaccess file. This needs to go before any WordPress rewrites.

RewriteRule ^(something)/(\d{3,6})-([a-zA-Z0-9-]+)\.html /$1/$3-$2 [R=302,L]

This will also pass-through the query string from the original request. If the query string should be stripped, then append a ? to the end of the RewriteRule substitution (or include the QSD flag if on Apache 2.4).

The number in the regex is literally just a string of digits between 3 and 6 chars long (inclusive). It's not literally 100 to 100000 numerically.

This is currently a 302 (temporary) redirect - which makes testing easier since it's not cached by the browser. Change this to a 301 (permanent) when you are sure it's working OK.

P.S. There are also some other links that should be skipped, cause we don't have a way to rework them...

If there are some URLs that follow this pattern but should be skipped then you will need to make some exceptions - a condition (RewriteCond) that comes before this rule. Something like:

RewriteCond %{REQUEST_URI} !^/something/123-exclude-this\.html
RewriteCond %{REQUEST_URI} !^/something/456-and-exclude-this\.html
RewriteRule ^(something)/(\d{3,6})-([a-zA-Z0-9-]+)\.html /$1/$3-$2 [R=301,L]

Or, if there is a pattern to the URLs that should be skipped then these can be combined with an appropriate regex.

The ! prefix on the CondPattern (2nd argument to the RewriteCond directive) negates the regex. So, the above ruleset basically says, if the request matches the stated pattern and is not this or that then redirect.


EDIT: If "something" is variable, then you can either put each alternative in the RewriteRule pattern:

RewriteRule ^(category/subcategory|category/sub/subsub)/(\d{3,6})-([a-zA-Z0-9-]+)\.html /$1/$3-$2 [R=301,L]

Or, depending on your URLs (and whether there would be any conflicts) you could make it entirely generic:

RewriteRule ^(.+)/(\d{3,6})-([a-zA-Z0-9-]+)\.html$ /$1/$3-$2 [R=301,L]

The $1, $2 and $3 are backreferences to the parenthesised sub patterns (captured groups) in the RewriteRule pattern. So, $1 is substituted with whatever (.+) matches against in the URL-path.

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

资料来源与适用范围

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

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

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