URL routing-rewrite issue |
[eluser]Animas[/eluser]
I could remove index.php using .htacess. I am developing a blog type application. My post links are like www.site.com/blog/post/12 (12 is the post id that has title "My post title!"). What I need is to achieve urls like www.site.com/blog/post/My-post-title! . In blog.php (controller) the post function is Code: function post() My main page shows list of all blogs and blog titles are hyper linked which takes to individual detailed post. Hyperlink code inside blog_view.php Code: <h3><a href="blog/post/<?=$row->id?>"><?=$row->title?></a></h3> Thanks.
[eluser]Animas[/eluser]
I think here I should use URI Routing. Now I want to show post links(which I have set) like www.domain.com/blog/post/12/My-post-title! to www.domain.com/My-post-title! . I guess i should put some regular expressions inside route.php. But can't figure out the exact expression. I want "My-post-title!" to call "blog/post/12/My-post-title!". I set Code: $route['([a-z]+)/(\d+)'] = "blog/post/$1"; Any suggestion?
[eluser]adamp1[/eluser]
If you want to remove the index.php file you MUST use .htaccess files. Now with the routing. By the looks of your regexp you are not taking count of either the - or the !. So it will not match. I would first advise that no title can have any char not allowed to be passed in the url, so that gets rid of the !. Now you say you want www.domain.com/post-title to call www.domain.com/blog/post/12/post-title. This is not possible since you are adding in information not currently in the URI. The only thing possible is something like this www.domain.com/post_title goes to www.domain.com/blog/post/post-title. Code: $route['([a-z_]+)'] = "blog/post/$1";
[eluser]Animas[/eluser]
[quote author="adamp1" date="1207491914"] The only thing possible is something like this www.domain.com/post_title goes to www.domain.com/blog/post/post-title. [/quote] You can see that function post() Code: $this->db->where('id', $this->uri->segment(3));//class/function/id, so segment(3) is id In blog_view.php I used Code: <h3><a href="blog/post/<?=$row->id?>/<?=url_title($row->title, 'dash')?>"><?=$row->title?></a></h3> Code: $this->db->where('title', $this->uri->segment(3)); Code: <h3><a href="blog/post/<?=url_title($row->title, 'dash')?>"><?=$row->title?></a></h3>//no id
[eluser]adamp1[/eluser]
Why do you need both the post id and the title? It would be better to use just the title (like wordpress can) then google can index the page better. Why not when getting the title out of url you remove the - chars and then turn them into spaces. That way you can use this to search the database
[eluser]adamp1[/eluser]
Another thing I would advice is to maybe use of underscores in the url_title(). Underscores are less likely to be used in a blog post title than a dash. You can then restrict the underscore from being in the title. This way you just need a nice little function to de_prep_url_title so you can use its information again. I.E replace _ with a space.
[eluser]Animas[/eluser]
Here is my updated code parts of blog.php Code: function post() parts of blog_view.php Code: <h1><?php echo $heading;?></h1> parts of post_view.php Code: <h1><?php echo $heading;?></h1> But still the same problem. When clicking title "My Page Heading" is printed but noting from that particular post. I used to see post only when I used id instead of title. When a title link is clicked in blog_view, it opens the post_view. Can't there be duplicate title? But id will be unique. It would be nice if you could suggest me what changes I should do inside this code.
[eluser]Pascal Kriete[/eluser]
[quote author="Animas" date="1207501924"]Can't there be duplicate title? But id will be unique. It would be nice if you could suggest me what changes I should do inside this code.[/quote] Yes the id will be unique. But if a user calls up www.example.com/blog/that_bbcode_tutorial and you have two posts called 'That bbcode tutorial' you won't know which one they want. There is no point to using both an id and a title. It adds nothing to the granularity of the selection. An id is unique, so that by itself is enough. If you want the title, then you have to work it so it can do the job by itself as well. The route you suggested would have to map the title to an id. If you can map the title to the id, you don't need the id in your url anymore. Let's dig into that code now. It looks correct as it is, so we'll just work on figuring out where it fails. My suggestion would be to ditch all the uppercases and spaces in the title, you can do that when inserting: Code: // $orig_title comes from a form submit or whatever..makes no difference Now to get this back, the user will need to enter that clean url. So if we called our post 'That GreaT adventUrE', the url segement has to be 'that-great-adventure' regardless. To get it back: Code: // Use the name in the url to call the db (just like you're doing now) If something isn't coming out as expected the first thing to do is print_r the array/object to see if the keys are set at all. This should ensure that your database selections don't fail because of case issues and odd characters. It doesn't solve the duplicate titles yet. One thing to do would be to add a number to the end of the title if it already exists.
[eluser]Animas[/eluser]
[quote author="adamp1" date="1207491914"] The only thing possible is something like this www.domain.com/post_title goes to www.domain.com/blog/post/post-title. Code: $route['([a-z_]+)'] = "blog/post/$1"; Entering post titles with "-" replacing " " solved the issue(duplication not solved yet). But routing as above has some problem. www.domain.com/post_title) alaways showing first post only even if it is www.domain.com/post_title_two link . |
Welcome Guest, Not a member yet? Register Sign In |