Remove question mark added to url using get get method

I use the “get” form and method to offer users the option of returning to the unknown URL that they came to my site, according to the code below. I prefer this only to the browser buttons and it works without javascript.

The problem I am facing is that some browsers (chrome, safari, maybe others) add a question mark to the end of the URL to which they link. I do not want this for SEO reasons.

My question is:

1) Can I prevent the question mark from inside my php code? or

2) Please can someone show me how to redirect the url using htaccess, I may 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>'; } ?> 
+6
source share
5 answers

? is probably added because you are making a GET request from the form.

Why not do something like:

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

use the POST method instead of GET

+1
source

How appropriate is the form to use for this? Why not use the hyperlink and style to make it look like you want with CSS?

You can use try with a button instead of creating an input value to be passed.

 <form action="url" method="get"> <button type="submit">Return to previous page</button> </form> 
0
source

Instead of sending to random URLs (which is actually not a good idea), we recommend using redirects

Decision will be

 <?php if (isset ($_SERVER['HTTP_REFERER']) ) { $url = htmlspecialchars($_SERVER['HTTP_REFERER'],ENT_QUOTES,'UTF-8'); echo '<br /><form action="redirect.php" method="get"> <input type="hidden" name="return" value="'.$url.'"> <div id="submit"><input type="submit" value="Return to previous page" /></div> </form>'; } ?> 

then in redirect.php

 <?php if (isset ($_GET['return'] ) { $url = $_GET['return']; header('Location: '.$url, true, 303); // or 302 for compatibility reasons echo '<a href="'.htmlspecialchars($url,ENT_QUOTES,'UTF-8').'">Continue</a>'; exit; }else{ echo 'Error, no redirect specified'; } 

you can replace action="GET" and $_GET with action="POST" and $_POST , and it will work the same.

or simply

 if (isset ($_SERVER['HTTP_REFERER']) ) { $url = htmlspecialchars($_SERVER['HTTP_REFERER'],ENT_QUOTES,'UTF-8'); echo '<a href="'.$url.'">Return to previous page</a>'; } 

both will still create a new story in the browser.

0
source

In htaccess, you should have something like this:

 RewriteEngine on RewriteRule ^myPage-([a-zA-Z0-9_]*).html myPHPfile.php?var=$1 

In the above part, added to your htaccess, when a URL like myPage-whatever.html called, it just looks like calling myPHPfile.php?var=whatever

-1
source

Source: https://habr.com/ru/post/944724/


All Articles