0

Let me explain what I mean:

I want to redirect from https://example.net/category/83745/my-first-post to https://myredirect.net/my-first-post but without considering /category/numbers/

For the moment I work with this:

if(window.location.pathname == '/category/83745/my-first-post')
{
    window.location.href="https://myredirect.net/my-first-post";
}

And it is working fine but as I described I need to remove /category/numbers/ because they could be different and only consider this part /my-first-post for the redirection.

Thanks in advance.

1
  • Note that you can do this on the server using .htaccess rewrite rules (that way the browser's address bar will still show the long URL though) Commented Jul 19, 2019 at 8:39

4 Answers 4

1

if you want to just ignore the first 2 parts dynamically and only care about the last part of the URL then just do the following:

var stringContains = function (str, partial){
    return (str.indexOf(partial) > -1);
};

var url = '/category/83745/my-first-post';


if(stringContains(url, "/category")){
   var parts = a.split("/");
   window.location.href = parts[parts.length-1];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use String's methods lastIndexOf and slice:

var path = window.location.pathname;
window.location.href = "https://myredirect.net" + path.slice(path.lastIndexOf('/') + 1);

Comments

0

Use Regex. Something like

if(window.location.pathname.match(/\/category\/\d+\/my\-first\-post$/)
{
    window.location.href="https://myredirect.net/my-first-post";
}

Comments

0

You can run a regular expression match on the pathname

if(window.location.pathname.match(/my-first-post$/)) {
    window.location.href='/my-first-post';
}

More on regexes: https://www.regular-expressions.info/

Another good tool for building and testing regexes: https://regex101.com/

Edit:

To give an example of how to regex according to the more fleshed out specs from Chris G

let pathmatch = window.location.pathname.match(/([^\/]+)$/g);
window.location.href = '/' + pathmatch[0];

Thus, regex can be utilized to grab any pattern and use it later. IF there is a need to make sure the pathname contains category and/or numbers, it is easily added in to the pattern. This one simply disregards anything before the last forward slash (/)

6 Comments

If you're going to literally state the pathname, why do a match() in the first place...?
@chris-g what do you mean "literally state", thats a real regex, matching only when the path ends with specified string. The op specificly said "but without considering /category/numbers/" so i purposefully left that part out. You could put .* in front but to what end?
Not only that, because it is a match(), you can put captures in that can later be used in the redirect. It is an extensible solution to a basic problem
Yeah, I know how regex works. What I mean is that not every blogpost is going to be called my-first-post obviously, so your solution is useless for any other path that needs to be rewritten, see OP's last sentence where he specifically mentions that a solution needs to remove the category and number no matter what the title slug at the end is. See hindmost's answer for the only correct one (out of four answers).
A regex based solution is of course possible but it would require a regex like /\/category\/\d+\/([A-Za-z-])$
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.