TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2007, 09:36 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default mod rewrite - how to get title in url?

Hey guys!

I have a php file names view_news.php. This PHP file gets the item id from the url and queries the news table in my DB to show the entry with that id.

The table has a title column. How can I make a mod rewrite file so I can have my title as the url eg: mydomain.com/news/id-news-title-goes-here.html?

I don't want to add each news item seperately into my htaccess file. How can I accomplish this and how can I get php to remove all weird symbols etc... from my url title?

Looking forward to your answers! :)
Sam Granger is offline  
Reply With Quote
Old 10-10-2007, 12:51 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 436
Thanks: 22
Karl is on a distinguished road
Default

Hi Sam,

Try creating a new .htaccess file and adding the following code:

Code:
<IfModule mod_rewrite.c>

	RewriteEngine on

	RewriteRule ^RewriteRule ^news/([0-9]+)-([^\/]+).html$ news.php?id=$1 [L]$

</IfModule>
What this should do is try to catch your news urls and then call news.php passing it the id from the url. As for protection, the in order for the url to be valid it must begin with news/ followed by a numeric value, followed by a hyphen and atleast one single letter, so for instance, this would be valid:

news/1-a.html

but this would not:

news/a-a.html

Hope this helps.
Karl is offline  
Reply With Quote
Old 10-10-2007, 01:27 PM   #3 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default

The htaccess file gives me a 500 server error. :(
Sam Granger is offline  
Reply With Quote
Old 10-10-2007, 02:17 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 436
Thanks: 22
Karl is on a distinguished road
Default

Hey, I somehow managed to mess up the copy and paste, lol :)

here's the .htaccess file again:

Code:
<IfModule mod_rewrite.c>

	RewriteEngine on

	RewriteRule ^news/([0-9]+)-([^\/]+).html$ news.php?id=$1 [L]

</IfModule>
Karl is offline  
Reply With Quote
Old 10-10-2007, 02:25 PM   #5 (permalink)
The Contributor
 
Join Date: Sep 2007
Posts: 89
Thanks: 1
Sam Granger is on a distinguished road
Default

Damn thanks! Works like a dream!
Sam Granger is offline  
Reply With Quote
Old 10-10-2007, 03:58 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 700
Thanks: 2
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
Code:
<IfModule mod_rewrite.c>

	RewriteEngine on

	RewriteRule ^news/([0-9]+)-([^\/]+).html$ news.php?id=$1 [L]

</IfModule>
You might want to change the RewriteRule a wee bit, at the moment it really isn't exactly what you want (or is it?). I'll explain why that's the case after showing you an improved version of that line: (the # line is just a comment)
Code:
# Original by Karl: RewriteRule ^news/([0-9]+)-([^\/]+).html$ news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)-[^/]+\.html$ news.php?id=$1 [L]
Changes:
  • Removed parenthesis in ([^/\]+).
    This is simply because we have no need to capture whatever was inside those brackets. In other words, we don't use $2 in the right-hand side of the rewrite rule.
  • Removed backslash from [^\/]+.
    There is no need to escape the forward slash character in this instance so what that really says is one or more characters which are not either a forward- or back-slash. We only want to negate the use of forward slashes (to prevent urls like: news/001-something/else/entirely.html).
  • Escaped the . (dot/period) immediately before "html".
    This is because the dot character is a wildcard meaning any single character. If the dot were not escaped you could have an url like: news/001-titlezhtml which satisfy the rewrite but obviously isn't what we're after here.

(Sorry for hijacking your thread with comments on Karl's code)
__________________
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:51 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0