SimonBruce Posted October 21, 2012 Share Posted October 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Zane Posted October 21, 2012 Share Posted October 21, 2012 <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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 21, 2012 Share Posted October 21, 2012 Also, .htaccess has nothing to do with how the links are generated. Only how the web server handles the URIs the browser sends. Quote Link to comment Share on other sites More sharing options...
JohnTipperton Posted October 21, 2012 Share Posted October 21, 2012 (edited) <a href="article.php/<?php $news->Title()?>"> you should put echo to <?php echo $news->Title()?> Edited October 21, 2012 by JohnTipperton Quote Link to comment Share on other sites More sharing options...
SimonBruce Posted October 21, 2012 Author Share Posted October 21, 2012 <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!! Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 21, 2012 Share Posted October 21, 2012 function Title(){ return htmlspecialchars($this->data['n_title']); } Quote Link to comment Share on other sites More sharing options...
SimonBruce Posted October 21, 2012 Author Share Posted October 21, 2012 (edited) 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 October 21, 2012 by SimonBruce Quote Link to comment Share on other sites More sharing options...
SimonBruce Posted October 21, 2012 Author Share Posted October 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.