TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   mod rewrite - how to get title in url? (http://www.talkphp.com/absolute-beginners/1282-mod-rewrite-how-get-title-url.html)

Sam Granger 10-09-2007 08:36 PM

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! :)

Karl 10-10-2007 11:51 AM

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.

Sam Granger 10-10-2007 12:27 PM

The htaccess file gives me a 500 server error. :(

Karl 10-10-2007 01:17 PM

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>


Sam Granger 10-10-2007 01:25 PM

Damn thanks! Works like a dream!

Salathe 10-10-2007 02:58 PM

Quote:

Originally Posted by Karl (Post 2976)
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)


All times are GMT. The time now is 10:26 PM.

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