views:

37

answers:

2

Edit...

Someone suggested I do it differently... If I were to use .htaccess, would this work?

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^chapter http://mysite.com/chapter/1

I'm just using basic javascript here, but I wanted to find out if there's a way to add it to my jquery file so that the redirection will happen if the path matches something specific?

Right now, I'm using:

<script type="text/javascript">window.location.replace("/chapter/1");</script>

I'd like that to only occur if the user goes to http://mysite.com/chapter.

I'm not altogether clear on how to get the path using jquery and do a pattern match?

+1  A: 

jQuery is a Javascript library which helps you manipulate DOM elements.
Since you're not manipulating DOM elements, jQuery won't do you any good.

You can use the location.href property:

if (location.href === '...')
    location.replace("/chapter/1");

Or,

if (/regex pattern/.test(location.href))
    location.replace("/chapter/1");

However, you should probably do this on the server instead.

SLaks
I see. You mean, just using like an .htaccess file? In which case, would the edit in my original post work?
N00BNum1
Did you try it?
SLaks
I did, but I quickly realized it won't exactly work... The "1" is dynamic, and I'm not sure how to pass that to an .htaccess file. Essentially, the "1" represents an ID. So, if a user goes to /chapter, they should be redirected to /chapter/[ID]...
N00BNum1
@Newt420: Then you can use server-side code (PHP or ASP.Net)
SLaks
A: 
$(document).ready(function(){

  if(window.location.href.match(/^.*\/chapter\/?$/)){
    window.location.replace("/chapter/1");
  }

});

See this question: http://stackoverflow.com/questions/2522532/how-can-i-use-javascript-to-match-a-string-inside-the-current-url-of-the-window-i

You can use the same regex in the htaccess file...

Keyne