2

We've recently made a change on one of our sites where a ton of URLs need to be redirected via htaccess but we also need to account for any additional information that may be appended after the URL.

I can get the specific URL to redirect as expected but not with additional characters afterward. I know that sometimes it's important what order rules come in within the file like this thread mentions but there shouldn't be any conflicting rules for these redirects.

These are all specific URLs, so this thread about matching a specific word in the URL structure won't work for our issue.

It doesn't look like the FOO/BAR/BAZ to FOO/NEW/BAZ example is applicable either.

I thought that our caching might be impacting it but other changes were implemented right away, so I think it's just a question of syntax

Current formatting example URL in our htaccess:

Redirect 301 /product/product-name/ https://example.com/announcements/

What Needs To Happen: Any incoming direct links that folks have for /product/product-name/ that include anything after the trailing slash such as /product/product-name/ref/123/ should also be redirected to /announcements/ but this is not the case, these types of links are going to our 404 page.

Attemped Solutions: So far (today - this isn't the first time I've tried this), I've tried the following to wildcard my 301's with no success:

SO question: Wildcard redirect at end of url in .htaccess

SO question: Wildcard RewriteRule for 301 redirects in htaccess

External reference: https://bootstrapcreative.com/how-to-redirect-known-pages-and-catch-all-for-everything-else-htaccess/

I'm open to suggestions & appreciate any advice!

Jeff W
  • 147
  • 1
  • 2
  • 13
  • You've not actually stated exactly what you are trying to do? What URL(s) are you trying to match? What is the expected target URL? Examples please. "we also need to account for any additional information that may be appended after the URL" - in what way do you need to "account" for this "additional information"? And what exactly is this "additional information"? Is this part of the URL-path? Or query-string? Or something else? – MrWhite Jan 30 '22 at 01:07
  • We also need to see the non-working directive you are using. We will also likely need to see the complete `.htaccess` file with this directive in place, whilst you state "there shouldn't be any conflicting rules" - conflicting rules are a very common cause of error. – MrWhite Jan 30 '22 at 01:07
  • Ah yes, sorry Mr. White, good call. Essentially we've redirected a large number of URLs to another site that we're active on and the exact URLs are redirected. We have an affiliate program on our site though and it occurred to us afterward that any affiliate links wouldn't be redirected, as an example /ref/123/ would appear at the end of the URL. This is going to a 404, currently. – Jeff W Jan 30 '22 at 01:10
  • The working rule would be like so: Redirect 301 /product/name/ https://example.com/announcements/ - and as long as it's the exact URL, it's fine but trying *any* of the provided examples I linked to in my OP doesn't do anything different. – Jeff W Jan 30 '22 at 01:13
  • (You need to edit your question to include that additional information - also to retain formatting.) However, that `Redirect` directive you posted in the comment should already do what you appear to require... it would redirect `/product/name/ref/123/` to `https://example.com/announcements/ref/123/`. Is that what you want to happen? Is that not what's happening? However, we need to see your actual rule in-place in your complete `.htaccess` file. Please edit your question to include this information. – MrWhite Jan 30 '22 at 01:21
  • Done! I was working on a project out back with my son & on my phone earlier, I know the comment format was, erm .. lacking. – Jeff W Jan 30 '22 at 01:25
  • "these types of links are going to our 404 page." - so, are you saying that no redirect occurs? Or a 404 occurs at the target site? Are you wanting to preserve this "affiliate" URL-path through the redirect (as the current rule _should_ do) or strip it off and redirect to `/announcements/` only (as would seem to be suggested by your edited question)? – MrWhite Jan 30 '22 at 01:34
  • NB: You should always test with 302 (temporary) redirects (and preferably with the browser dev tools open and caching disabled). 301 (permanent) redirects are persistently cached by the browser so can make testing very problematic. – MrWhite Jan 30 '22 at 01:36
  • It results in a 404 on the target URL, anything after the trailing slash, for this purpose, should be stripped out entirely. For clarification, the redirect is internal, with /announcements/ residing on the site with the redirects. – Jeff W Jan 30 '22 at 01:39

1 Answers1

2
Redirect 301 /product/product-name/ https://example.com/announcements/

The mod_alias Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, for example, a request for /product/product-name/ref/123/ would be redirected to https://example.com/announcements/ref/123/ (since ref/123/ is what's left of the URL-path after the match against /product/product-name/). It would seem to be the target URL, ie. https://example.com/announcements/ref/123/, that is causing the 404, so the redirect is happening.

To redirect /product/product-name/<anything> to https://example.com/announcements/ only then you need to use the RedirectMatch directive instead, which matches using a regex, not simple prefix-matching.

For example:

RedirectMatch 301 ^/product/product-name/ https://example.com/announcements/

This will redirect as follows:

  • /product/product-name/ to https://example.com/announcements/
  • /product/product-name/ref/123/ to https://example.com/announcements/
  • /product/product-name/<anything> to https://example.com/announcements/

If you need this to be more specific, eg. only match optional affiliate links, eg. /ref/<number>/, rather than literally anything, then do something like the following instead:

RedirectMatch 301 ^/product/product-name/(ref/\d+/)?$ https://example.com/announcements/

As noted in comments, you should test first with a 302 (temporary) redirect to avoid potential caching issues. And you will need to clear your browser cache before testing, since the erroneous 301 (permanent) redirects will have been cached by the browser.

However, if you are already using mod_rewrite (RewriteRule) directives elsewhere in the config file then you may need (or would be preferable) to use mod_rewrite for this redirect instead.

Reference:

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    Works like a charm, Mr. White. Many thanks! **Also** vm appreciate the 301 vs 302 tip, will definitely keep that in mind. – Jeff W Jan 30 '22 at 01:56