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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-25-2007, 02:52 PM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default Mod_rewrite

I want to rewrite some url's using mod_rewrite. All of the posts in my cms have a unique id so to use a url like http://www.mysite.com/article/1 would be easy enough, however I would rather do it like http://www.mysite.com/article/article_title if I can.

Is this possible? how would I do it?
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 03:02 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

It would be possible but each article title would need to be unique. A good alternative is to do it like:

http://www.mysite.com/article/1/article_title

This way also means you don't have to store the SEF URI or create a set of functions for handling them.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-25-2007, 03:19 PM   #3 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

So with this method the 'article_title' on the end isn't really needed as the actual location of the article is given by the id, the 'article_title' part is just for better seo, is that right?
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 03:22 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

yep, correct.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-25-2007, 03:38 PM   #5 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Gotya lol, I'll give it a shot.

Thanks.
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 04:02 PM   #6 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Can anyone see where I'm going wrong with this?

PHP Code:
RewriteEngine On
RewriteRule 
^article/(.*)/(.*) /view.php?action=view&id=$1&article=$
It should be redirecting from www.mysite.com/view.php?action=view&id=14 to www.mysite.com/articles/14/article_name but I just get a 404 error.
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 04:22 PM   #7 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

RewriteRule ^articles/(.*)/(.*) /view.php?action=view&id=$1&article=$2

Problem is highlighted
bluesaga is offline  
Reply With Quote
Old 10-25-2007, 04:53 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

There's still a problem with that Regex string, the first grouping (first set of parentheses) will match everything:
www.mysite.com/articles/14/article_name would lead to:
$_GET['id'] = "14/article_name"
It's just simple regular expressions and a more appropriate one would be somewhere along the lines of:
Code:
RewriteRule ^articles/([0-9]+)/([^/]+)/?$ view.php?action=view&id=$1&article=$2 [QSA,L]
If you need things explaining, just ask. :)
Salathe is offline  
Reply With Quote
Old 10-26-2007, 08:02 AM   #9 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

or depending on your config you can make it non-greedy like so:

RewriteRule ^articles/(.*?)/(.*?) /view.php?action=view&id=$1&article=$2

But i would suggest using salathe's for some reason i figured you were using non-greedy parentheses
bluesaga is offline  
Reply With Quote
Old 10-26-2007, 08:03 AM   #10 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

If you dont mind showing me what each part does that would be great so I know what I'm doing lol :)

Where should I be placing this .htaccess file? Normally I would put it in the htdocs folder but because I'm creating a simple cms in a folder called 'cms' I thought I would be better off putting it in that folder, but it doesn't seem to work :S
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-26-2007, 08:19 AM   #11 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

You need to use a rewrite base if you are going to put it in a sub-folder like so:

Code:
RewriteEngine On
RewriteBase   /cms
RewriteRule ^articles/([0-9]+)/([^/]+)/?$ view.php?action=view&id=$1&article=$2 [QSA,L]
If you do not want to place this in a sub-folder and want to have it in the root folder you need to add prefixes to the urls like so:

Code:
RewriteEngine On
RewriteBase   /
RewriteRule ^cms/articles/([0-9]+)/([^/]+)/?$ view.php?action=view&id=$1&article=$2 [QSA,L]
bluesaga is offline  
Reply With Quote
Old 10-26-2007, 08:38 AM   #12 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thats great, thanks :D
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 09:34 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design