开发者

Redirecting a URL based on first 2 letters in its URL alias

So I have a problem in which I'm attempting to redirect a URL based on whether or not it begins with a certain few letters or numbers. I'm needing to redirect a site visitor if the first 2 of the trailing URL begins with "A2" or "a2", remove the first 2 of the URL and then replace the "A2" or "a2" with a specific directory path.

So here's a specific example to clarify. If the user clicks on (or types in their browser's address bar)

http://www.example.com/A2d39g

or

http://www.example.com/a2d39g

they would automatically be redirected to

http://www.example.com/product/d39g

Also worth nothing, they individual typing in this URL will be reading开发者_高级运维 it off a label in the mail, they won't always be clicking on a link. I originally had thought of doing this through jQuery (note some of the solutions below), but I've modified this posting as I think it would be better to approach this through the .htaccess file.


See a demo of the following here.

The first thing we need to do is bind the click event and prevent the default behavior:

$('.redirMe').click(function(e){
    e.preventDefault();
});

Then, within this click function we'll perform a series of splits and substring divisions in order to test the code value. Note that I assume the link is no more complicated than what you've given, that it will always be the root host followed by a simple alphanumeric code.

var th   = $(this).attr('href'), // get the href
    ah   = th.split('/'),        // split it up
    code = ah[ah.length - 1],    // grab the last value (the code)
    c2   = code.substring(0, 2), // grab the first two code chars

Then we'll pop the code off the end of the split href to get the host and test the first two characters converted toLowerCase to see if they match a2, and if so redirect to product, otherwise visit a 404 page:

redr = (c2.toLowerCase() == "a2") ?
       ah.join('/') + '/product/' + code.substring(2) :
       ah.join('/') + '/404;

You can then set window.location.href = redr to redirect!

Again, here's sample code of the above.


You can do this kind of redirection with Apache mod_rewrite. Add something like this to your httpd configuration files:

RewriteEngine On
RewriteRule ^/[aA]2(.*)$|^/(.*)[aA]2$ /product/$1


There is no direct way to do this in JavaScript in a cross-browser manner; many browsers do not allow you to get the full URL from window.location in order to prevent malicious JavaScript from tracking users. The best way to do this would be on the server side. If you want to do a redirection from JavaScript, you would probably need to send an AJAX request to a server and hope that the HTTP referer header indicated the page where it was coming from, so it could respond with the desired action.


I found the solution...I reposted the question slightly differently here and it was answered: Redirect a URL based off it's first character with htaccess

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜