![]() |
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? |
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. |
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?
|
yep, correct.
|
Gotya lol, I'll give it a shot.
Thanks. |
Can anyone see where I'm going wrong with this?
PHP Code:
|
RewriteRule ^articles/(.*)/(.*) /view.php?action=view&id=$1&article=$2
Problem is highlighted |
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: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] |
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 |
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 |
You need to use a rewrite base if you are going to put it in a sub-folder like so:
Code:
RewriteEngine OnCode:
RewriteEngine On |
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