Jump to content

Clean Urls - Replacing '%20' With A '-' Help!


SimonBruce

Recommended Posts

Hi guys,

 

I'm new to this site so thought I'd start off with a question regarding something I've been wanting to fix for ages!!

 

We have a custom CMS on our website that produces URLs based on the title of the news article and leaves spaces between the words.

 

For example an article called 'News article title' creates a link like this: <a href="article.php/<?php $news->Title()?>">

 

Which results in the browser seeing a link like this: http://www.ourwebsite.com/article.php/news article title

 

and once clicked turns to this in the browser: http://www.ourwebsite.com/article.php/news%20article%20title

 

Obviously i'd prefer it to look like this: http://www.ourwebsite.com/article.php/news-article-title

 

I have played around with .htaccess and can get it to change the gaps into dashes, but seeing as the page isn't titled with dashes the link fails...

 

I'm guessing I need to modify my link in the PHP, but this is where my lack of experience shows!!

 

Any advise on how I can change the above to make the URL's cleaner and more SEO friendly would be appreciated!!

 

Simon

Link to comment
Share on other sites

<a href="article.php/<?php $news->Title()?>">

According to your code, the method Title() does the echo. Using OOP, you do not echo data inside the class. You strictly return data. This way, you can do something like this

 

<a href="article.php/<?php echo str_replace(' ', '-', $news->Title()) ?>">

 

So, go into your class file and change your Title method to return $variable instead of echo $variable.

Link to comment
Share on other sites

<a href="article.php/<?php $news->Title()?>">

According to your code, the method Title() does the echo. Using OOP, you do not echo data inside the class. You strictly return data. This way, you can do something like this

 

<a href="article.php/<?php echo str_replace(' ', '-', $news->Title()) ?>">

 

So, go into your class file and change your Title method to return $variable instead of echo $variable.

 

This looks promising! Have changed my link to include your suggested code, but not sure how to amend the class to work.

My class for the title looks like this:

 

 

function Title(){

echo htmlspecialchars($this->data['n_title']);

}

 

any thoughts?

 

Thanks!!

Link to comment
Share on other sites

function Title(){
return htmlspecialchars($this->data['n_title']);
}

 

That's sorted the display of the links so thanks!

Only problem is that although I'm now directed to the page with a nice clean name in the browser eg /Article-Title instead of /Article%20Title the page isnt rendering and i am presented with a template page without content. If i manually put the %20 back in, it fixed the link and the content appears. Am i missing something in my class or on the article page itself?

 

This is the head of my article page:

 

 

<?php

require_once '../include/config.php';

require_once '../class/news.php';

 

$id = substr($_SERVER['PATH_INFO'],1);

 

 

$news = new News();

$news->Get($id);

$news->Latest();

 

 

?>

Edited by SimonBruce
Link to comment
Share on other sites

May also be the $id part of the class???

 

 

function Get($id){

//No data yet, so start query

$this->db = new Database();

$this->db->Execute("SELECT n.*,DATE_FORMAT(n_date,'%D %M %Y') AS fdate FROM news n INNER JOIN users u ON u.id = n.user_id WHERE site_id='{$this->SiteId}' AND n.n_title='".$this->db->Escape($id)."'");

}

 

Sorry... i'm totally lost with my PHP, but it's clear that now the title is being displayed with '-' instead of '%20' the page isnt pulling in content...

In particular above i can see that the id for the page is based on a few things including the n_title which we are now displaying differently?

 

Any thoughts would be appreciated :)

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.