1

I was just wondering if anyone could enlighten me with some knowledge. What I want to do is to "overlap" an url. For example:

website.com/index.php?page=something&id=5432

becomes (taking the id from the first url):

website.com/src/popup.php?page=something&id=5432

Any quick and simple way to do this?

Thanks!

jasonscript
  • 6,039
  • 3
  • 28
  • 43
John Dough
  • 13
  • 2
  • Is this, like, a browser plugin you want to make? Or are you developing this site? Kinda hard to tell what you're working with. In terms of string manipulation, this seems pretty doable; regexes might be the most straightforward way depending on how familiar you are with them. – Katana314 Jul 28 '15 at 02:14
  • Sorry for being a little vague. It is indeed a browser plugin. – John Dough Jul 28 '15 at 12:20

1 Answers1

0

It seems like you you're looking for some kind of redirection here.

I'm not sure if you're thinking of using entirely javascript here, as you've shown a PHP file extension.

Javascript

For a Javascript-only solution, you might try something like this:

//Parameter function thanks to https://stackoverflow.com/a/901144/917035
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

//Build the new location string.
var page = getParameterByName("page");
var id = getParameterByName("id");
var locationString = 'http://website.com/src/popup.page?page=' + page + '&id=' + id;
//Redirect
window.location = locationString;

Alternatively you could achieve the same thing with PHP:

PHP

<?php 
    $page = $_GET['something'];
    $id = $_GET['id'];
    $locationString = 'website.com/src/popup.page?page=' . $page . '&id=' . $id;
    header('Location: ' . $locationString);
?>

Which would take the parameters in your URL query (page and id) and then pass them into the header request to redirect the user away.

If you're looking for a long-term solution for all pages, there are options available in the ways of editing your .htaccess files, similar to how this answer uses a RewriteRule to preserve the parameters.

Community
  • 1
  • 1
Singular1ty
  • 2,577
  • 1
  • 24
  • 39
  • 1
    Thanks a lot for your help, but I failed to mention that I do not own the website I intend to make change to. – John Dough Jul 28 '15 at 11:13
  • @JohnDough That's okay! The javascript code there should still work though, I would imagine? Most browser plugins have access to all of the javascript features and window control too. – Singular1ty Jul 28 '15 at 22:36