Thommy Tomka
Thommy Tomka

Reputation: 332

301 redirect with regular expression - special case

This topic is far to abstract for me and I fear I will never really understand how to create the right pattern.

I do have 1500++ URLs which I need to redirect with a 301. This is far to much to do manually, so there MUST be a solution.

The website contains URLs like:

somedomain.tld/somepath/

somedomain.tld/somepath/somefile.ext

somedomain.tld/somepath/somepath/text-0123456

somedomain.tld/somepath/somepath/some-more-text-0120123

somedomain.tld/somepath/somepath/numbers-1234-7654321

What I need to redirect are the last 3 types of URL. The new target should look almost the same, but without the last numbers and the last stroke.

IE:

OLD: somedomain.tld/somepath/somepath/text-0123456

NEW: somedomain.tld/somepath/somepath/text

OLD: somedomain.tld/somepath/somepath/some-more-text-0120123

NEW: somedomain.tld/somepath/somepath/some-more-text

OLD: somedomain.tld/somepath/somepath/numbers-1234-7654321

NEW: somedomain.tld/somepath/somepath/numbers-1234

Saying it in words the redirect should select all URLs ending with a "-" AND a 7 digits number, and redirect them to the same URL without that "-" and without that 7 digits number.

Thanks in advance for Your kind support! Thomas

Upvotes: 0

Views: 57

Answers (2)

Ωmega
Ωmega

Reputation: 43703

RewriteCond %{HTTP_HOST}:%{SERVER_PORT}s ^(.*):(443(s)|\d+s)$
RewriteRule ^/?(.+)-[0-9]{7}$ http%3://%1/$1 [R=301,QSA,L]

will take care of redirection, keeping same protocol and possible GET parameters...

Upvotes: 1

anubhava
anubhava

Reputation: 786101

This can definitely be solved by a regex based rule. Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^(.+?)-[0-9]{7}$ /$1 [L,R]

Upvotes: 2

Related Questions