2

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...

1

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.

  • Yes, your assumptions are correct. Its basically a standard WP link. Words are separated with single hyphen, number is a automatically generated post number. One more question. This "something" part, it can be category/subcategory or even category/sub/subsub... What should I put in that palce? Or should I put separate rule for every combination? – klo Mar 1 '16 at 8:53
  • You could have separate rules (most specific first), although that could result in unnecessary repetition. You can alter the RewriteRule pattern to accommodate variable URL-paths (categories). I've updated my answer. Incidentally, I've also updated the previous directives to allow for this (to avoid writing "something" twice in both the pattern and substitution). – MrWhite Mar 1 '16 at 9:23
  • Is this working for you? – MrWhite Mar 3 '16 at 14:43
  • yep, sorry for late response.... – klo Mar 5 '16 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.