1

I want to forward URLs (with parameters) from my old domain to URLs (without parameters) on my new domain.

The URLs look like this:

  • old-domain.com/deceased-persons-2021/?id=1500_betty-white (id=1500_ has to be gone)

  • new-domain.com/deceased-persons/2021/betty-white

The problem is, that I don't have to manage only the value id=1500_betty-white of the parameter as backreference, but also the year 2021, because I have very many URLs.

If the URLs would be only old-domain.com/deceased-persons-2021 OR only old-domain.com/deceased-persons/?id=1500_betty-white, I knew the solutions. But I don't know how to manage the backreference in the URL in combination with the backreference in the parameter. How could I solve this?

2
  • Have to ask. What happens if there are two people with the same name? Commented Mar 10 at 18:05
  • A very good question. Indeed, deleting the unique id is not a very safe solution. But it's not so bad as it seems, because there is also the year of death in the URL, which makes the URL different. The truth is, that the persons don't really have an own physical page, e.g. based on a custom post type. These pages are virtual pages. I'm using the database of my third website, which contains the real physical pages of these persons, and generate these pages on the fly. So, usually there will be no identical slugs, because in case of same names, one of them has an additional digit in the slug. Commented Mar 10 at 19:17

1 Answer 1

0

Since you have changed the URL (and changed the domain) this would need to be an external "redirect", as opposed to a "rewrite" (or "forward") since you need to redirect the user/search engine/bot.

I am assuming that old-domain.com and new-domain.com point to different servers. If not then you'll need an additional condition (RewriteCond directive) that checks the requested hostname (HTTP_HOST).

If the URLs would be only old-domain.com/deceased-persons-2021 OR only old-domain.com/deceased-persons/?id=1500_betty-white, I knew the solutions.

I would have thought that if you knew how to do these redirects separately then you would know how to do them together since you basically just combine them, so it would have been interesting to see how you would have solved these separately.

Capturing part of the URL-path (ie. 2021) from the RewriteRule pattern generates a backreference of the form $n (where n is a number 0-9). Whereas capturing part of the query string (which requires a preceding condition) generates a backreference of the form %n. You then use $n and %n in the substitution string.

For example:

# /deceased-persons-2021 to /deceased-persons/2021 RewriteRule ^deceased-persons-(\d{4})$ https://new-domain.com/deceased-persons/$1 [R=301,L] 

The $1 backreference contains the captured 4-digit year from the end of the URL-path.

# /deceased-persons/?id=1500_betty-white to /deceased-persons/betty-white RewriteCond %{QUERY_STRING} ^id=\d+_([a-z-]+)$ RewriteRule ^deceased-persons/$ https://new-domain.com/deceased-persons/%1 [QSD,R=301,L] 

The %1 backreference contains the captured name from the preceding condition (RewriteCond directive). In this example, the name part can only contain lowercase letters a-z and hyphens.

The QSD flag is necessary to discard the original query string.

Combining these two rules...

# /deceased-persons-2021/?id=1500_betty-white to /deceased-persons/2021/betty-white RewriteCond %{QUERY_STRING} ^id=\d+_([a-z-]+)$ RewriteRule ^deceased-persons-(\d{4})/$ https://new-domain.com/deceased-persons/$1/%1 [QSD,R=301,L] 

Personally, I would also capture the deceased-persons part of the URL-path to avoid repetition. For example:

RewriteCond %{QUERY_STRING} ^id=\d+_([a-z-]+)$ RewriteRule ^(deceased-persons)-(\d{4})/$ https://new-domain.com/$1/$2/%1 [QSD,R=301,L] 

This rule would need to go at the top of the root .htaccess file at old-domain.com.

This assumes that the name (in the query string) consists of just lowercase letters and hyphens. If, however, there can be "an additional digit in the slug" then you'll need to modify the regex accordingly.

Test first with a 302 (temporary) redirect to avoid potential caching redirect and only change to a 301 when you have confirmed this works as intended. 301 (permanent) redirects are cached persistently by the browser so can make testing problematic.

5
  • Perfect, thanks! I didn't know that the backreference for QUERY_STRING has to be %n, so now it's clear. Unfortunately the persons names of the old URL paths will not only contain lower case letters, but also special characters and upper case letters. Is there a better way to catch all letters? Could you explain the using of HTTP_HOST? I will transfer all my websites to another provider. They are managed under different subscriptions, but on the same server with Plesk. Do I have to take it into account? And the tipp to use 302 instead of 301 is great. I was wondering, why my tests failed ;-) Commented Mar 11 at 8:28
  • To be more precisely. Assumed the name in the URL-path is 'Paul-Cézanne', I had to do in a second step a conversion of the query-backreference from 1) upper to lower and 2) from 'é' to 'e'. How could I do this? Commented Mar 11 at 12:03
  • In another homepage of mine I'm using this rules to change special characters in URLs generally. But how can I implent them into the mentioned parameter backreference? RewriteCond %{REQUEST_METHOD} !=POST RewriteCond %{REQUEST_URI} ^(.*?)(E|É|é|Ê|ê|È|è|Ð|ð|Ë|ë|Ē|ē|Ĕ|ĕ|Ė|ė|Ę|ę|Ě|ě)(.*?)$ RewriteRule . %1e%3 [R=301,L] Commented Mar 11 at 20:44
  • I've meant "implement"... Commented Mar 11 at 20:51
  • If I would use these rewrite rule in htaccess of the NEW domain, the characters in the forwarded URL are converted correctly. But is this a correct forwarding from old to new domain? Assumed the old URL is old-domain.com/deceased-persons-1907/?id=1500_Paul-Cézanne, then the first 301 would lead to new-domain.com/deceased-persons/1907/Paul-Cézanne, so it's landing on the new domain with wrong letters. Is this bad for Google or page ranking? Or is the forwarding nevertheless ok, if the mentioned last rewrite rule is converting the special characters at the new domain, if the URL is called? Commented Mar 12 at 7:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.