TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Mod_rewrite (http://www.talkphp.com/general/1340-mod_rewrite.html)

Jmz 10-25-2007 02:52 PM

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?

Karl 10-25-2007 03:02 PM

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.

Jmz 10-25-2007 03:19 PM

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?

Karl 10-25-2007 03:22 PM

yep, correct.

Jmz 10-25-2007 03:38 PM

Gotya lol, I'll give it a shot.

Thanks.

Jmz 10-25-2007 04:02 PM

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.

bluesaga 10-25-2007 04:22 PM

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

Problem is highlighted

Salathe 10-25-2007 04:53 PM

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

bluesaga 10-26-2007 08:02 AM

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

Jmz 10-26-2007 08:03 AM

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

bluesaga 10-26-2007 08:19 AM

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]


Jmz 10-26-2007 08:38 AM

Thats great, thanks :D


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

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