-1

Im struggling to figure out the best way to redirect/rewrite urls with some pattern matching using javascript.

BACKGROUND:

I have a blog filtered by tag: http://adrtimesv6.squarespace.com/library/?tag=The+Psychology+%26+Neuroscience+of+Mediation (select view as visitor if that comes up)

I have another area in the site called "Collections" where i load in groups of posts from the library in its own real page with other content in a more controlled environment with page titles and urls that are better for seo, like this: http://adrtimesv6.squarespace.com/collections/the-psychology-and-neuroscience-of-mediation. the posts are loaded in via a query that pulls in posts from the library that have a tag that matches the name of the collection page. this is working pretty well. the problem im running into is that when someone filters the blog posts by tag i want them to be redirected to the collection page rather than go to the blog filter page.

QUESTION:

What im trying to figure out is how to use jquery to redirect all the /library/?tag=[path] urls to /collection/[path] urls. To do this i think i need to use jquery to:

1) swap "/libary/?tag=" to "/collections/"

2) revise the rest of the path from the tag formatted path "The+Psychology+%26+Neuroscience+of+Mediation" to my collection formatted path: "the-psychology-and-neuroscience-of-mediation"

ADDITIONAL INFORMATION:

I am using a CMS so i dont have access to things like the .htaccess file. in the CMS admin settings there is a url redirect panel that allows me to list some basic redirects in this format: -> ... but trying this with the tag filter urls is not working.

Im mostly concerned with this redirect occuring when the user clicks on the tags listed at the bottom of the blog post - so i could use jquery to modify the href attribute in the tag listed for the blog post. just not sure how to write the pattern matching and rewrite the format correctly. ive tried a bunch of things but cant seem to get it.

or perhaps use window.location.replace somehow?

UPDATE: In my particular case, i found that i could update my template code and json formatters so that the tag link href generated matches the url in the collection section - so my problem is solved ....however, i still wonder how i would do handle this for other situations.

basically, 1) how to use javascript or jquery to check if the url (either href on the page or the browser url) contains something, then replace that with something else, and then modify the full url so that it is a simple dashed url .... takes out all + signs and replaces them with - and removes the %26 for & characters to return a simple url string, etc, like: /here-is-the-path

VUELA
  • 268
  • 1
  • 7
  • 22
  • URL rewriting has to be done server side, not client side. – A. Wolff Aug 08 '13 at 20:09
  • @roasted - i dont think you understand what im asking. 1) i dont have access to server side so thats not an option, 2) there should be a way to target the href attribute on the filter blog by tag links so that it does the two steps i outlined above and takes the user to the collection page i created instead of the built-in filter by tag page -- OR some way to check the page url & redirect using javascript. – VUELA Aug 08 '13 at 20:59
  • perhaps i can do something like: 'if (path.match("/library/?tag="))' -- use that to check the tag link href and then swap the matched part of the path to "/collections/" that might take care of the first part of the puzzle. -- then i would need to figure out how to store the rest of the path, change its format to match the collection links format and then apply that to make the new href that should be used? – VUELA Aug 08 '13 at 21:04
  • If i understand what you mean, try: `$(function () { $('a.tag.link[href^="/library/?tag"]').attr('href', function () { return '/collections/' + $(this).attr('href').split('=').pop().toLowerCase().replace(/\%26/g, 'and') }); });` – A. Wolff Aug 08 '13 at 21:30
  • This could be worth looking at: http://stackoverflow.com/questions/18254699/javascript-split-url – yaqoob Sep 30 '13 at 14:12

1 Answers1

0

On a single page, you can use this to forward a visitor to a new page:

window.location = "http://www.google.com"

You could add logic to use the same script on every page. For example:

if ( document.location == "http://adrtimesv6.squarespace.com/library/?tag=The+Psychology+%26+Neuroscience+of+Mediation" ) {
  window.location = "http://adrtimesv6.squarespace.com/collections/the-psychology-and-neuroscience-of-mediation"
}

and so on. You could even add more advance string manipulation to handle every URL.

But doing a JavaScript redirect in this manner will not have positive SEO effects such as friendly URLs. It will not enable your web server to handle the new URL; if http://adrtimesv6.squarespace.com/collections/the-psychology-and-neuroscience-of-mediation returns an error from your web server, no amount of front end JavaScript will be able to fix that.

Doing this with JavaScript this would probably be a negative experience for your users, because they would first have to download the page from the first URL, then execute the JavaScript, and then download the page from the new URL.

KatieK
  • 13,586
  • 17
  • 76
  • 90
  • since im only using this redirect for the blog's filter by tag urls (that dont get crawled or listed by search engines anyway) im not too concerned with those kind of seo issues. also if i use jquery to revise the href for the tag link on the page instead of actually redirecting the url then there is not seo issue at all. – VUELA Aug 08 '13 at 20:52
  • also note the info here on using window.location.replace in order to avoid some issues regarding keeping referrer data in tact, keeping the users history working, etc: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript/506004#506004 – VUELA Aug 08 '13 at 20:53
  • my question is not how to simply redirect one page to another using javascript, it is how to use javascript to revise the url or the href links for these blog filtered by tag links in a DYNAMIC way, so that they all redirect without having to list each one. i need help figuring out the pattern matching. – VUELA Aug 08 '13 at 20:55
  • @VUELA - So you're just working on the "advanced string manipulation" component? What have you got so far? – KatieK Aug 08 '13 at 22:22