Search code examples
.htaccesshttp-redirectwildcard

How can I wildcard redirect everything after a certain URL in htaccess?


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!


Solution

  • 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: