Jump to content

Recommended Posts

So I'm trying to figure out how to get the url's of my dynamic web pages to look nicer and be more search friendly.

The dynamic pages are based on info in a mysql database. In the database is a row that stores titles of each page.

my question is, how to get the titles from the database to be in the url? instead of, for example, /display.php?id=23 etc.

also a second question related to the first... since the titles are written with spaces in between each word, how to replace the blank spaces with hyphens?

 

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/
Share on other sites

hi,

ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible?

hi,

ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible?

 

hello,

sure it is possible. It's pretty much same as dreamwest wrote. Just create a htaccess rule like

 

RewriteRule ^Pretty_url/(.*)/(.*) display.php?id=$1

 

and then you can use urls like

http://www.site.com/Pretty_url/23/whatever-topic-title-goes-here

with the id of the title accessible by $_GET['id'] in display.php file. If you want to get rid of the ID in url completely you would need to provide unique identifier for each topic .. say

My Topic would use my-topic as identifier and you can then change the htaccess rule to something like

 

RewriteRule ^Pretty_url/(.*) display.php?identifier=$1

 

and then link to http://www.site.com/Pretty_url/my-topic and access the topic by it's identifier $_GET['identifier'] that's equal to 'my-topic' in this case. Just make sure you would use only legal characters in the urls. Hope that helps ;)

 

 

 

a-scripts that does help a lot.

i a couple follow up questions though.

Is there a way to automatically remove illegal characters?

and also is there a way to limit the amount of words that

get taken from the database?

some of the titles are really long and if it were possible to only pass up to a maximum amount using

$_GET[id] it would be ideal in this situation.

 

Thanks.

Add another column in your database to hold a url slug.

 

When you do your insert, build the slug with all the conditions that you want to use on it (so use str_replace() to strip characters or words you don't want to show up, and trim the string so it doesn't go over a certain length).

 

You can then call your pages using the slug instead of the title or the id number, and use your rewrite condition as normal :)

hi,

ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible?

 

No sure what you mean but theres always a simple answer

 

Your hyperlinks should look like :

 

http://site.com/Pretty_url/id number

 

Not:

 

http://site.com/display.php?id=$1

 

For forms use a redir.php instead of direct

 

	
<form name="searchForm" action="/redir.php" method="GET">

<INPUT class=search_box tabIndex=1 maxLength=128 name="id"  value="" size="31">

<input type="submit"  value="Search">
</form>

 

And in redir.php:

*This will handle all your str replace stuff so you can add extras to the string

 


$search=$_REQUEST['id'];

if($id == '' ) {

echo "ERROR!!";

//Or just redirect
header("Location: index.php"); 

}else{

$search= str_replace("-"," ",$search);
header("Location: Pretty_url/$search"); 
}

 

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.