How to Redirect based on page?
05-19-2010, 03:21 PM
|
How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
I need to redirect pages on my site to other pages based on the page name on my site.
My site is a Url shortener, when you type in a website it assigns it a key using mysql db starting with 1 then 2 etc. How do I make the site redirect a user coming to my site with the link, http://mydomain.com/1 to the full url assigned to the key '1'?
Thanks
Aadam
|
|
|
|
05-19-2010, 03:23 PM
|
Re: How to Redirect based on page?
|
Posts: 15
|
Using header location:
http://php.net/manual/en/function.header.php
Code:
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
|
|
|
|
05-19-2010, 03:26 PM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
But how do I relate that to the url in the database that matches the page?
Thanks
|
|
|
|
05-19-2010, 03:35 PM
|
Redirection regarding database
|
Posts: 193
Name: Adam Moseley
|
I need to redirect pages on my site to other pages based on the page name on my site.
My site is a Url shortener, when you type in a website it assigns it a key using mysql db starting with 1 then 2 etc. How do I make the site redirect a user coming to my site with the link, http://mydomain.com/1 to the full url assigned to the key '1'?
Thanks
Aadam
|
|
|
|
05-19-2010, 03:49 PM
|
Re: Redirection regarding database
|
Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
|
read the database for the URL, send a redirect instruction to the useragent.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
05-19-2010, 03:52 PM
|
Re: How to Redirect based on page?
|
Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
05-20-2010, 04:06 AM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
This is part the code I am using:
Code:
if(isset($_GET['k'])) {
$k = mysql_real_escape_string($_GET['k'], $db);
if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
header('HTTP/1.1 301 Moved Permanently'); -------line 24
header('Location: ' . $row[0]); ----------25
exit;
}
}
But I get these errors below, any ideas?
Warning: Cannot modify header information - headers already sent by (output started at /home/moseleya/public_html/zumco.co.uk/index.php:9) in /home/moseleya/public_html/zumco.co.uk/index.php on line 24
Warning: Cannot modify header information - headers already sent by (output started at /home/moseleya/public_html/zumco.co.uk/index.php:9) in /home/moseleya/public_html/zumco.co.uk/index.php on line 25
|
|
|
|
05-20-2010, 04:58 AM
|
Re: How to Redirect based on page?
|
Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
|
You cant send headers ( header('Location:....... ) in the middle of a script unless you are using output buffering.
It is as simple as:
PHP Code:
<?php ob_start();
//File contents....
ob_end_flush();
|
|
|
|
05-20-2010, 05:17 AM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
I dont understand where to put that though, sorry im new to this.
|
|
|
|
05-20-2010, 05:21 AM
|
Re: How to Redirect based on page?
|
Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
|
open any .php file with one (or more) of those header redirects.
Place ob_start(); on line 2, after <?php.
then place ob_end_flush(); on the 2nd from last line, followed by ?>
|
|
|
|
05-20-2010, 05:50 AM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
It still delivers the same errors, just different line numbers as its moved
|
|
|
|
05-20-2010, 05:55 AM
|
Re: How to Redirect based on page?
|
Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
|
can you paste your whole script?
Also, is this script being included into another file? The output buffering needs to start at the very beinging of the script.
Example:
index.php
-- included file somthing.php
-- included file redirects.php
So here, index.php includes somthing.php and somthing.php includes redirects.php, the output buffering needs to be done in index.php at line 2.
|
|
|
|
05-20-2010, 11:21 AM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
This is all the script
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
ob_start();
$baseurl = 'http://zumco.co.uk/'; //address to use as the first part of the short URLs (note trailing slash)
$sql_host = 'localhost'; //host of your mysql database (usually localhost)
$sql_user = 'moseleya_moseley'; //mysql user with access to your database
$sql_pass = 'pass'; //password for this user
$sql_db = 'moseleya_turl'; //the name of your database
$db = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_select_db($sql_db, $db);
if(isset($_GET['k'])) {
$k = mysql_real_escape_string($_GET['k'], $db);
if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $row[0]);
exit;
}
}
}
if(isset($_POST['submit'], $_POST['url'])) {
if(filter_var($_POST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false) {
$url = mysql_real_escape_string($_POST['url'], $db);
if($result = mysql_query("SELECT `key` FROM `turl` WHERE `url` = '" . $url . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
$key = $row[0];
}
}
if(!isset($key) && mysql_query("INSERT INTO `turl` (`url`) VALUES ('" . $url . "')", $db)) {
$id = mysql_insert_id($db);
$key = base_convert($id, 10, 36);
mysql_query("UPDATE `turl` SET `key` = '" . $key . "' WHERE `id` = '" . $id . "'");
}
echo '<a href="' . $baseurl . $key . '" target="_blank">' . $baseurl . $key . '</a>';
}
}
else {
?>
<form method="post" action="index.php">
<label>URL: <input type="text" name="url" id="url" /></label>
<br />
<input type="submit" name="submit" id="submit" value="Shorten" />
</form>
<?php
}
ob_end_flush();
?>
</body>
</html>
There are no includes its just a one page script, the site is hosted at zumco.co.uk in my sig.
Cheers
|
|
|
|
05-20-2010, 11:27 AM
|
Re: How to Redirect based on page?
|
Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
|
THIS->
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
Is OUTPUT, and the header() call HAS TO BE before any output.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
05-20-2010, 11:37 AM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
Ok, I no longer have any error messages but the page does not redirect to the full URL when visited.
Have I done something wrong in the script?
Ps I used Link for the tutorial.
|
|
|
|
05-20-2010, 11:49 AM
|
Re: How to Redirect based on page?
|
Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
|
I would assume it will come down to the info in the database but without seeing it im only stabbing in the dark.
|
|
|
|
05-20-2010, 12:37 PM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
Do you know if there is code to redirect a page based on its name?
I.e. If they shorten the URL and it goes to zumco.co.uk/45 can i forward that to the full url that relates to the key 45?
|
|
|
|
05-20-2010, 12:58 PM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
as the /xxxxx bit relates to the key in the database which in turn is associated with a full url.
|
|
|
|
05-20-2010, 01:13 PM
|
Re: How to Redirect based on page?
|
Posts: 193
Name: Adam Moseley
|
Code:
if(isset($_GET['k'])) {
$k = mysql_real_escape_string($_GET['k'], $db);
if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $row[0]);
exit;
That is the part of the code that is supposed to redirect the page.
Please note that turl is the table in the database that everything is located.
Where is the problem??
|
|
|
|
05-20-2010, 01:16 PM
|
Re: How to Redirect based on page?
|
Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
Originally Posted by aadam
Do you know if there is code to redirect a page based on its name?
I.e. If they shorten the URL and it goes to zumco.co.uk/45 can i forward that to the full url that relates to the key 45?
|
Script the 404 page,
extract the number from the failed URI,
get the URLfrom the database for entry 45,
redirect
Job sorted.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
Last edited by chrishirst; 05-20-2010 at 01:21 PM..
|
|
|
|
|
« Reply to How to Redirect based on page?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|