Jump to content

Trying to Rewrite For Domain.com/Title


MarcyS

Recommended Posts

I've been trying for hours to rewrite my current url:

 

domain.com/post.php?id=7

 

...and make it appear like this:

 

domain.com/this-is-my-title

 

I haven't been able to do it yet. All the online examples show using an id first or something, but I really like just .com/title only. 

 

So.. can I do that? I've seen other sites achieve it, such as upworthy, so I know it's possible. 

 

Part II: 

 

How would I call the new page from index.php? I have a database table called 'article' and the column I need is 'title' and there's obviously a postid for each entry.. 

 

I can manage to use php enough to put a dash in between all the words of the title and remove all special characters but... Not sure how to make the call though. I mean... I see that $theArticle['title'] returns the title in my current code, but displaying the url only? 

 

Is what I'm trying possible?  Thanks for any suggestions!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/283426-trying-to-rewrite-for-domaincomtitle/
Share on other sites

You can't do that directly. Rewriting works by taking pieces of the URL and rearranging them into something you can use with your PHP script. That means all you have to work with (in your example) is "this-is-my-title". You can't conjure up the id=7 from that.

 

However your PHP script can. If you must use "/this-is-my-title" then the simplest course of action is to modify your post.php so that it can take a title as well as/instead of an ID number. /post.php?title=this-is-my-title

Well at least I won't beat my head against the wall over this anymore. Thanks for letting me know that it can't really be done this way. I modified htaccess to allow for post/id/title.. and that's much better than what it was. 

Thanks for the reply! 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.