Jump to content


SEO Friendly urls


15 replies to this topic

#1 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 18 September 2007 - 01:00 PM

Im trying to make seo friendly urls to a custom script but not any kind of urls i want to make them like wordpress
example:
blahblah.com/year/category/news-name-here.html
i want to make something like that
i took a look in wordpress but its kinda hard for me because im a beginner in php
the php must get the request url and read the data then query from mysql and display the page... i think this is the way its done.
if someone can write me here a small example it could help me alot :)
Thank you in advice and sorry for my bad english

Edited by Carnacior, 18 September 2007 - 01:04 PM.


#2 austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 18 September 2007 - 01:29 PM

What you want is mod_rewrite which deals with the .htaccess file, not php. Check out this tutorial!

-Austen :)

#3 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 18 September 2007 - 02:04 PM

i think you didin`t get my idea :)
it uses htaccess and php :D
from
blahblah.com/2007/sport/soccer-news-test.html
goes to
blahblah.com/news.php?year=2007&?cat=sport&?id=6
it gets from blahblah.com/2007/sport/soccer-news-test.html
edit: this is just an example
at the beggining of news.php it has to be some php what reads the request url and reads the requested data ( news in this case ) and takes a query into the database and then it shows the output whit the requested data :)
do you understand now ?
if not then take a look to a wordpress ( blog script ) demo or soething you gonna se what dynamic urls has :)
its very helpfull for seo when you got a big site

Edited by Carnacior, 18 September 2007 - 02:05 PM.


#4 _*C++_*

  • Guests

Posted 18 September 2007 - 05:05 PM

It is pretty simple actually. Try something like this.
<?php
$title = $_GET['title'];
$year = $_GET['year'];
$cat = $_GET['cat'];

$page = mysql_query("SELECT * FROM `table_name` WHERE `year` = '$year' AND `cat` = '$cat' AND `title`= '$title' LIMIT 1");

if (mysql_num_rows($page) == 0)
{
	 echo "Sorry the page you are looking for cannot be found.";
}else{
	 $r = mysql_fetch_array($page);
	 echo "Title: " . $title . " posted in " . $year . " in category " . $cat;
}

?>
Then for the URL's you would have to do .htaccess to get them to work. Hope that helps.

Edited by C++, 20 September 2007 - 02:13 AM.


#5 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 18 September 2007 - 05:27 PM

that reads this news.php?year=2007&?cat=sport&?id=6
:) the links gonna be generated from a script i want to make it fully automated ... i don`t want to add a line in htaccess for each page... nobody knows how wordpress dynamic urls work ? :(
php has to read the accesed url blahblah.com/2007/sport/soccer-news-test.html and strip only the data
/$data1/$data2/$data3.html if you know what i mean :)


sorry for my bad english but i don`t know how to explain better

#6 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 18 September 2007 - 09:01 PM

View Postausten, on Sep 18 2007, 01:29 PM, said:

What you want is mod_rewrite which deals with the .htaccess file, not php. Check out this tutorial!

-Austen :D


View PostCarnacior, on Sep 18 2007, 02:04 PM, said:

i think you didin`t get my idea :)
it uses htaccess and php :)
from
blahblah.com/2007/sport/soccer-news-test.html
goes to
blahblah.com/news.php?year=2007&?cat=sport&?id=6
it gets from blahblah.com/2007/sport/soccer-news-test.html
edit: this is just an example
at the beggining of news.php it has to be some php what reads the request url and reads the requested data ( news in this case ) and takes a query into the database and then it shows the output whit the requested data :(
do you understand now ?
if not then take a look to a wordpress ( blog script ) demo or soething you gonna se what dynamic urls has :)
its very helpfull for seo when you got a big site

Actually, he got it exactly like you wanted.

You use mod_rewrite commands in .htaccess

http://www.webmaster-toolkit.com/mod_rewri...generator.shtml

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
That's what WordPress adds to .htaccess

Edited by SpatialVisionary, 18 September 2007 - 09:04 PM.


#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 September 2007 - 11:44 PM

Seconding what Spatial said but going a bit further.

Mod_rewrite uses regular expressions (regex) to match what you want from the url dynamically. A good tutorial for this can be found at the following link: http://www.webmasterstop.com/125.html

Of course, regex is a fairly universal system (note: fairly, not entirely), if you want the php break down, you can check out the PCRE section of the manual.

Example for you:
/year/category/news-name-here.html <- Basic url pattern, in english.
^/[0-7]{4}/[a-zA-Z0-9_]+/(.*)\.html$ <- Slightly less than basic url pattern, in regex.

Of course, if spatial is right, and that is all the .htaccess that wordpress uses, then it's probably all php (apparently handled through index.php).
$url = explode('/', $_SERVER['REQUEST_URI']);
$year = $url[0];
$category = $url[1];
$news_name = substr($url[2], 0, -5); // Assuming no query string or anchors are used

Edited by rc69, 18 September 2007 - 11:48 PM.


#8 _*C++_*

  • Guests

Posted 20 September 2007 - 02:16 AM

To get the effect you are trying to desire you will have to do mod_rewrite to get the links to work. But to make the actually links you will want to do something like this.
<?php
$links = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
while($r = mysql_fetch_array($links))
{
	 echo '<a href="http://www.yoursite.com/' . $r['year'] . '/' . $r['cat'] . '/' . $r['title'] . '.html</a>' . $r['title'] . '</a><br />\n';
}
?>

Edited by C++, 20 September 2007 - 04:55 PM.


#9 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 20 September 2007 - 09:30 AM

i love you guys :)
one more question...
how i can strip -
for example blah-blah-blah :)
i want the php to delete them and remain only blah blah blah

Edited by Carnacior, 20 September 2007 - 09:56 AM.


#10 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 20 September 2007 - 10:36 AM

depending on what exactly you want to do with it, you can do a few things.

str_replace() or explode()

$str = "blah-blah-blah";

$blahs = explode("-", $str);
Then you have each 'blah' in a slot in an array and can do whatever you want with it.

#11 _*C++_*

  • Guests

Posted 20 September 2007 - 04:56 PM

The best idea would just be this. I'm assuming you want to take those out of the title correct but I would highly recommend you leave them in for the URL unless the pages will display something like
http://www.yoursite....lah%20blah.html
$new = str_replace("-"," ",$r['title']);
Then just put the $new variable between the <a></a> tags. Hope that helps ^_^

#12 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 22 September 2007 - 04:27 PM

Thank You Guys ! You helped me alot :( i finally managed to integrate this into e107 CMS :D it works like charm... i got to fix some small problems...
Thankx again !
edit:
a small question
if i use to many str_replace it will slow down the page render ? i use about 4 for a link :( and when the page whit the dinamyc url is rendered it loads up very very slow :(

Edited by Carnacior, 22 September 2007 - 05:26 PM.


#13 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 22 September 2007 - 07:56 PM

If you are doing 4 str_replaces one after the other then you can combine them into one str_replace.

$str = 'my name is matt';
$new_str = str_replace( 'name', 'cats name', $str );
$new_str = str_replace( 'matt', 'morgan', $str );
echo $new_str;

That example can be broken down to:

$str = 'my name is matt';
echo str_replace( array( 'name', 'matt' ), array( 'cats name', 'morgan' ), $str );

Matt

#14 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 23 September 2007 - 08:43 AM

10x
i got other problems now :P
php include and image locations messed up because of
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
:P
have a look
http://mijto.com/stiri/Extern/Bin_Laden_a_...u_la_rampa.html
sorry my site is in romanian

#15 Carnacior

    Young Padawan

  • Members
  • Pip
  • 17 posts
  • Location:Romania
  • Interests:webmaster

Posted 27 September 2007 - 07:47 AM

nobody can help me ?

#16 fiv3isaliv3

    Young Padawan

  • Members
  • Pip
  • 258 posts
  • Gender:Male

Posted 27 September 2007 - 08:51 AM

I'm pretty sure it is because you haven't added this...

<base href="http://www.domain.com" />

Add it in the head of the index and of course switch href location to where the index file is located

Edited by zealivity5, 27 September 2007 - 08:54 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users