Jump to content

seo url article page links issue


lovephp

Recommended Posts

friends so i created seo friendly blog works ok but i face a issue which is say i in menu put links like index.php | contact.php all these works find but in my article.php it don't work if i do not add full url like say http://localhost/blog/index.php

 

hope you get what im trying to say?

 

here is my article.php page

 

$url = $_SERVER['REQUEST_URI'];
$url = str_replace('/blog/', '', ucwords($url));

//$sql=mysql_query("SELECT id,title,keyword,description,image,youtube,article,created,views  FROM article WHERE url='".$url1."'");

$stmt = $db->prepare("SELECT id,title,keyword,description,image,youtube,article,created,views  FROM article WHERE url = :url");
$stmt->bindParam(':url', $url, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();
$count = $stmt->rowCount();
//$count = mysql_num_rows($sql);
if($count <1){
        header("Location: ".$site_path."index.php");
        exit();
}
$qry = "UPDATE article SET views = views +1 WHERE url = :url";
$stm = $db->prepare($qry);
$stm->bindParam(':url', $url, PDO::PARAM_STR);
$stm->execute();

//mysql_query("UPDATE article SET views = views +1 WHERE url ='".$url."'");
 //$row=mysql_fetch_array($sql);  

 

and my htaccess

RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\?*$ article.php?$1 [L,QSA]

how do i like every other page use links like index.php etc without having to use http://localhost/blog/index.php in my article.php page please someone.

Edited by lovephp
Link to comment
Share on other sites

One thing I notice is you are saving full youtube urls in a database? Just save the video id. I have a pile of functions for extracting just the id all varieties of youtube urls. If need something for that, PM me.

 

Now for the code...

 

Are you upper casing the words in the url and then looking to replace a lower cased /blog/ with nothing?

$url = str_replace('/blog/', '', ucwords($url));

 

There is a case insentitive version of that. str_ireplace()

 

For the rewrite, if don't want to view the directory /blog/ in the url then replace it something like this rule.

RewriteCond %{THE_REQUEST} ^GET\ /blog/

RewriteRule ^blog/(.*) /$1 [L,R=301]
Link to comment
Share on other sites

 

One thing I notice is you are saving full youtube urls in a database? Just save the video id. I have a pile of functions for extracting just the id all varieties of youtube urls. If need something for that, PM me.

 

Now for the code...

 

Are you upper casing the words in the url and then looking to replace a lower cased /blog/ with nothing?

$url = str_replace('/blog/', '', ucwords($url));

 

There is a case insentitive version of that. str_ireplace()

 

For the rewrite, if don't want to view the directory /blog/ in the url then replace it something like this rule.

RewriteCond %{THE_REQUEST} ^GET\ /blog/

RewriteRule ^blog/(.*) /$1 [L,R=301]

i get your point bro but ill once again try to explain this. all seems to be working ok but its the relative links on the particular article.php which is

 

<?php
$url = $_SERVER['REQUEST_URI'];
$url = str_replace('/blog/', '', ucwords($url));

$stmt = $db->prepare("SELECT id,title,keyword,description,image,youtube,article,created,views  FROM article WHERE url = :url");
$stmt->bindParam(':url', $url, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();
$count = $stmt->rowCount();

if($count <1){
        header("Location: ".$site_path."index.php");
        exit();
}

$qry = "UPDATE article SET views = views +1 WHERE url = :url";
$stm = $db->prepare($qry);
$stm->bindParam(':url', $url, PDO::PARAM_STR);
$stm->execute();
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<?php

echo "<a href="index.php">Home</a>";
echo "<a href="somepage.php">Home</a>";

?>

<body>
</body>
</html>

 

if you see in the body i added some links in relative manner and what happens is that those links becomes like http://localhost/blog/article/10/index.php

 

now the article/10 is something i store in db as whole along with the url for the topic like

 

article/10/this-is-some-topic-for-post

 

now why on earth and from where the article/10/ gets added every relative links i cannot figure out but if i add absolute links like http://localhost/blog/index.php then all seems ok

Link to comment
Share on other sites

The user's browser (all browsers) need an absolute url to request something from the Internet. When the browser gets a relative url, it uses the current page to determine the absolute url for the request. Since you are doing rewrites on the server side the browser thinks the current url is www.yourdomain/article/10/this-is-something, so it drops the "this-is-something" (because that's the "file" name) and keeps the rest as the "directory" name; then tacks the relative url on the end - so it has www.yourdomain/article/10/index.php.

 

In short, change the relative urls, to absolute urls. You don't have to include the domain, just put a slash at the front: /index.php since that goes to the document root for the current domain.

Link to comment
Share on other sites

The user's browser (all browsers) need an absolute url to request something from the Internet. When the browser gets a relative url, it uses the current page to determine the absolute url for the request. Since you are doing rewrites on the server side the browser thinks the current url is www.yourdomain/article/10/this-is-something, so it drops the "this-is-something" (because that's the "file" name) and keeps the rest as the "directory" name; then tacks the relative url on the end - so it has www.yourdomain/article/10/index.php.

 

In short, change the relative urls, to absolute urls. You don't have to include the domain, just put a slash at the front: /index.php since that goes to the document root for the current domain.

It was that simple?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.