Regular expression & routing problem |
[eluser]Animas[/eluser]
I am developing a blog type app. www.site.com shows all blog posts. Function "blog/index" shows blog list, function "blog/post" shows individual post. I removed index.php with .htaccess. I need to route www.site.com/post-title-a to www.site.com/blog/post/post-title-a. (assuming post-title-a is the title for first post, post-title-b for the 2nd post...) I used the following code in routes.php. Code: $route['([a-z0-9-]+)'] = "blog/post/$1"; Now when I go to www.site.com/blog/post/post-title-a the first post is viewed fine. But when I go to www.site.com/blog/post/post-title-b it also shows the first post. In fact all post links shows the first post page only. What could be wrong here? Thanks.
[eluser]Animas[/eluser]
Please have a look at my code for any possible mistake I have made. Still I couldn't solve the issue. blog.php Code: <?php blog_model.php Code: <?php blog_view.php Code: <html> part of config.php Code: $config['base_url'] = "http://localhost/ci/"; part of routes.php Code: $route['default_controller'] = "blog"; .htaccess Code: #Options -indexes db table: entries fields of entires: id, title, body ("title" field should take entries like "post-title-a")
[eluser]bcorcoran[/eluser]
Make sure that your scaffolding trigger in your routes is before the custom route, I didn't see that part of your routes.php... that may be the issue.
[eluser]Animas[/eluser]
[quote author="brendanc" date="1207637927"]Make sure that your scaffolding trigger in your routes is before the custom route, I didn't see that part of your routes.php... that may be the issue.[/quote] brendanc it is in place. I didn't put it here because I thought it might not be relevant. But I learned a new thing from you. Thanks. I still can't find out why all the post links fetches the first post only. (without routing all the post links fetches the respective posts)
[eluser]bcorcoran[/eluser]
Hrm, ok. Well, you should consider that your regular expression is going to be a bit "greedy" when it comes to parsing the URL in this way. I'll take a look at it myself and see if I can't come up with a Regex that works, but my fear is that you'll have to do some additional processing of the url input if you want to access anything but posts. My thought is that with your current Regex, you're going to be capturing (inside the brackets): www.example.com/[blog-post-title] www.example.com/[blog/post/id] www.example.com/[othercontroller/function] www.example.com/[anothercontroller/function/id] All with that one Regex... and they're all going to be passed to the /blog/post/$1 in your route. This is the problem with a greedy Regex So, like I said, you may need to route your URL to a separate controller and parse the URL from there and handle it accordingly... but I may be getting ahead of myself... I'll take a look at it
[eluser]bcorcoran[/eluser]
Also, try replacing this in your .htaccess: Code: RewriteRule ^(.*)$ index.php?$1 [L] With: Code: RewriteRule ^(.*) index.php/$1
[eluser]Animas[/eluser]
Thank you brendanc. I have changed .htaccess as you said but the issue remains unsolved. sql of db (blog) Code: CREATE DATABASE `blog` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; My application will show list of blogs(links) on www.site.com and any links www.site.com/any will show post links. It will a real help for a novice for me to have a solution for this issue. NB: a slight change in my routing rule to allow numbers in title Code: $route['([a-z0-9-]+)'] = "blog/post/$1";
[eluser]Ryuuzaki92[/eluser]
[quote author="Animas" date="1207737388"] Code: $route['([a-z0-9-]+)'] = "blog/post/$1"; this will catch everything. i think you should hijack the 404 function to reroute to the controller and method. but i'm not sure...
|
Welcome Guest, Not a member yet? Register Sign In |