最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript can't find my mod_rewrite query string! - Stack Overflow

programmeradmin4浏览0评论

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: .html?opt=login

I need / 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?

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./signinup.html?opt=login

I need http://www.example./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?

Share Improve this question asked Nov 8, 2010 at 9:50 Kyle CureauKyle Cureau 19.4k23 gold badges77 silver badges104 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

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.

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.

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论