Remove question mark added to url using form 'get' method

11,793

Solution 1

The ? probably gets added because you're doing a GET request from a form.

Why not do something like:

<input type="button" onclick='document.location.href=<?php echo json_encode($url);?>'>;

Solution 2

use POST method instead of GET

Share:
11,793
martin
Author by

martin

merge keep

Updated on June 16, 2022

Comments

  • martin
    martin almost 2 years

    I am using a form and the 'get' method to offer users a return option to an unknown url that they came from within my site, as per the code below. I prefer this to just the browsers back button and it works without javascript.

    The problem I am having is that some browsers (chrome, safari, there may be others) are adding a question mark to the end of the url they are referred back to. I don't want this for seo reasons.

    My question is either:

    1) Can I prevent the question mark within my php code somehow; or

    2) Please could somebody show me how to redirect the url using htaccess, I potentially have urls that could end:-

    .html?
    
    .htm?
    
    .php?
    
    /?
    

    Thanks in advance.

    <?php
    if (isset ($_SERVER['HTTP_REFERER']) ) {
    $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
    echo '<br /><form action="' . $url . '" method="get">
    <div id="submit"><input type="submit"  value="Return to previous page" /></div>
    </form>';
    }
    ?>
    
  • martin
    martin almost 11 years
    thanks, but I particularly want it to work without javascript
  • martin
    martin almost 11 years
    I don't know the url in advance?
  • Halcyon
    Halcyon almost 11 years
    Well, you could always use POST :/
  • martin
    martin almost 11 years
    You are correct that GET causes the question mark. I wasn't sure about using POST. Could there be an issue if the destination page already has a POST form for another purpose?
  • Halcyon
    Halcyon almost 11 years
    No, it shouldn't cause an issue but it's not exactly pretty either.
  • Nishan
    Nishan over 10 years
    ... which does totally not help answering the question. Yes, that's how you use RewriteRules, but it does absolutely nothing to remove question marks from the end of the called URL.