View Full Version : htaccess redirects for URLs with arguments
alankeys
11-25-2016, 07:24 PM
Question about 301 redirect / modrewrite
I know there are a lot of topics on this subject but so far I have not managed to create my needed redirect.
here is my current URL : mysite.net/doc.php?title=john
needs to redirect to : mysite.net/doc.php?renamed=john&id=3340
so am adding 'id' and renaming the variable 'title'.
does anyone know if this can actually be done ?
alankeys
12-06-2016, 11:46 AM
Question about 301 redirect / modrewrite
I know there are a lot of topics on this subject but so far I have not managed to create my needed redirect.
here is my current URL : mysite.net/doc.php?title=john
needs to redirect to : mysite.net/doc.php?renamed=john&id=3340
so am adding 'id' and renaming the variable 'title'.
does anyone know if this can actually be done ?
Would appreciate any comments or advice on above... Thanks.
alankeys
12-06-2016, 11:59 AM
Would appreciate any comments or advice on above... Thanks.
jscheuer1
12-06-2016, 02:48 PM
Assuming you tried:
Redirect 301 /doc.php?title=john http://mysite.net/doc.php?renamed=john&id=3340
?
alankeys
12-07-2016, 12:48 PM
Thanks John.
URL Parameters are not allowed at all in 301 redirects to the best of my knowledge.
The following has been suggested but I still cant make it work...
RewriteEngine on
RewriteCond %{QUERY_STRING} ^([^&]*&)*title=([^&]+)
RewriteRule ^/stock-charts/doc.php$ http://www.mysite.co.uk/stock-charts/doc.php [this part is unclear] [L,R=301]
Its as close as I have got...
styxlawyer
12-07-2016, 02:35 PM
Have you tried this online tool?
http://www.htaccessredirect.net/redirect-file-directory
jscheuer1
12-07-2016, 10:49 PM
Perhaps a PHP redirect would be more applicable. Does your host support PHP?
alankeys
12-08-2016, 09:39 AM
thnks for comments
xtyx it doesnt work for URLs with parameters.
jscheuer1 Have you any idea what the code would be ??
jscheuer1
12-08-2016, 05:07 PM
This appears to work, it would have to be the first thing on doc.php:
<?php
if(isset($_GET['title']) && $_GET['title'] === 'john'){
header('Location: ./doc.php?renamed=john&id=3340');
exit;
}
?>
Or:
<?php
if(isset($_GET['title']) && $_GET['title'] === 'john'){
header("HTTP/1.1 301 Moved Permanently");
header('Location: ./doc.php?renamed=john&id=3340');
exit;
}
?>
Depending upon the exact reason you're doing this, you might want to be more specific, like eliminating the possibility of all other post and get values, and/or be more general by redirecting all title to renamed, and possibly having a lookup for what each name would have as id.
alankeys
12-08-2016, 06:10 PM
John
Im going to look at these but 'id' is also to be fetched from the database. Im imagining that the code needs to also fetch 'id' that is related to 'title'.
also would like to contain these in a separate file that resides with the applicable directory/.
jscheuer1
12-08-2016, 07:15 PM
I'm pretty sure you could have it in a separate file, that file would have to be included on doc.php as the first thing. You can do virtually anything you want within the if section though, like call up data and so forth, as long as it's not output to the page as text. Once you write to the page, you can no longer redirect.
Yep, include works fine. Here's my redirect.inc page:
<?php
if(isset($_GET['title']) && $_GET['title'] === 'john'){
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $_SERVER['PHP_SELF'] . '?renamed=john&id=3340');
exit;
}
?>
And now, on doc.php all I need is:
<?php
include 'redirect.inc';
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.