Mobile app version of vmapp.org
Login or Join
Gloria169

: Htaccess rewrite incoming links to new one 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

@Gloria169

Posted in: #Htaccess #ModRewrite #Redirects #Wordpress

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:
example.com/something/XXXXX-some-text.html --> 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...

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Gloria169

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

example.com/something/XXXXX-some-text.html -->
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 //- [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 //- [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 //- [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$ //- [R=301,L]


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme