Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript can't find my mod_rewrite query string!

I use the following javascript class to pull variables out of a query string:

getUrlVars : function() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

So this works: http://example.com/signinup.html?opt=login

I need http://www.example.com/login/ to work the same way. Using mod_rewrite:

RewriteRule ^login/? signinup.html?opt=login [QSA]

allows the page to load, the javascript to load, the css to load, but my javascript functions can't find the opt key (i.e., it's undefined). How do I get opt to my javascript?

like image 899
Kyle Cureau Avatar asked Nov 08 '10 09:11

Kyle Cureau


5 Answers

Javascript is client-side. Mod_rewrite is server-side.

Therefore Javascript will never see the rewritten URL. As far as your browser is concerned, the URL that you entered is the finished address.

The only real solution is to change your Javascript so it looks at the URL it's got rather than the old version (or possibly parse for both alternatives, since the old URL will still work and people may still have old bookmarks).

The other possible solution would be to go to your server-side code (PHP? whatever?) where you can see the rewritten URL, and insert some javascript code there which you can parse on the client side. Not an ideal solution though. You'd be better of just going with option 1 and changing you Javascript to cope with the URLs it's actually going to be getting.

like image 72
Spudley Avatar answered Nov 06 '22 20:11

Spudley


Your issue is that JavaScript runs on the client side, so it will never see the ?opt=login part to which the URL gets converted internally on the server.

Apart from changing your regular expression to match the new URL format, the easiest workaround might be to write a JavaScript statement on server side that introduces the value of the opt variable into JavaScript.

like image 23
Pekka Avatar answered Nov 06 '22 20:11

Pekka


If you're using PHP, you can have the PHP create a JavaScript variable for you. For example:

$params = "?";

foreach($_GET as $key => $value) {
    $params = $params . $key . "=" . $value . "&";
}

echo 'var urlParams = "' . $params . '"';

Now, you JavaScript will have access to a urlParams variable that looks like this

?opt=login&

Then, in your Javascript code, wherever you expected to use the URL parameters, use the urlParams instead.

like image 44
Sanjay Avatar answered Nov 06 '22 21:11

Sanjay


If it's a special case, then put it as a special case in some way. If you rewrite generally, change your general regular expression. The way mod_rewrite works, the client never knows the rewritten URL. From the client, it's /login/ and /login/ only. Only the server ever knows that it's really signinup.html?opt=login. So there's no way your regular expression or location.href can know about it.

like image 34
Chris Morgan Avatar answered Nov 06 '22 21:11

Chris Morgan


Unless you use the [R] flag in your RewriteRule, the browser (and thus javascript) will never know about the new URL. If you don't want to be redirecting people, you're going to have to add some code to your login page that GET parameters as javascript in the page.

like image 29
Chris Avatar answered Nov 06 '22 20:11

Chris