Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>';
}
?>
like image 418
martin Avatar asked May 10 '13 15:05

martin


People also ask

How do I remove a question mark from a query string?

indexOf method to get the index of the question mark. To remove a querystring from a url, call the slice() method on the url and get the characters from index 0 , up to the index of the question mark in the string. The slice method will return a new string that doesn't contain the querystring. Copied!

How do I change the question mark in URL?

If you're using PHP for a server-side language, you can use something like... $nice_url = urlencode("http://your.old.url"); Other languages will have similar functions build in (or you can find one online). This will take care of your question mark (and other URL issues).

What is after question mark in URL?

The question mark ("?", ASCII 3F hex) is used to delimit the boundary between the URI of a queryable object, and a set of words used to express a query on that object. When this form is used, the combined URI stands for the object which results from the query being applied to the original object.


2 Answers

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);?>'>;
like image 135
Halcyon Avatar answered Sep 28 '22 19:09

Halcyon


use POST method instead of GET

like image 38
Amir Avatar answered Sep 28 '22 21:09

Amir