Welcome Guest, Not a member yet? Register   Sign In
URL routing-rewrite issue
#1

[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()
    {
        $this->db->where('id', $this->uri->segment(3));//class/function/id, so segment(3) is id
        $data['query'] = $this->db->get('entries');
        $this->load->view('post_view', $data);
        
    }


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/&lt;?=$row->id?&gt;">&lt;?=$row->title?&gt;</a></h3>
What changes do you suggest here so that I can achive urls like www.site.com/blog/post/My-post-title!

Thanks.
#2

[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";
but it is not working. (I am not good at regex)

Any suggestion?
#3

[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";
#4

[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
        $data['query'] = $this->db->get('entries');
where I am matching th post id.

In blog_view.php I used
Code:
<h3><a href="blog/post/&lt;?=$row->id?&gt;/&lt;?=url_title($row->title, 'dash')?&gt;">&lt;?=$row->title?&gt;</a></h3>
which get's that post id and shows the post. Without using post id how can I open the associated post? If I use
Code:
$this->db->where('title', $this->uri->segment(3));
        $data['query'] = $this->db->get('entries');
and
Code:
<h3><a href="blog/post/&lt;?=url_title($row->title, 'dash')?&gt;">&lt;?=$row->title?&gt;</a></h3>//no id
it shows the page heading but not anything of the post. url_title() added "-"s inside title.
#5

[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
#6

[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.
#7

[eluser]Pascal Kriete[/eluser]
In fact, that nice little function already exists. Going both ways actually.

One thing to watch out for is duplicate titles. It's probably better to add some sort of date functionality in the same way Wordpress does.
#8

[eluser]Animas[/eluser]
Here is my updated code
parts of blog.php
Code:
function post()
    {
        $data['title'] = "My Page Title";
        $data['heading'] = "My Page Heading";
        $this->db->where('title', $this->uri->segment(3));
        $data['query'] = $this->db->get('entries');
        $this->load->view('post_view', $data);
        
    }

parts of blog_view.php
Code:
<h1>&lt;?php echo $heading;?&gt;</h1>
&lt;?php if ($query->num_rows() > 0): ?&gt;
    &lt;?php foreach($query->result() as $row):?&gt;
    <h3><a href="blog/post/&lt;?=$row->title?&gt;">&lt;?=$row->title?&gt;</a></h3>
    <p>&lt;?=$row->body?&gt;</p>
    <hr />
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;

parts of post_view.php
Code:
<h1>&lt;?php echo $heading;?&gt;</h1>
    
&lt;?php if ($query->num_rows() > 0): ?&gt;
    &lt;?php $row = $query->row(); ?&gt;
    
    <h3>&lt;?=$row->title?&gt;</h3>
    <p>&lt;?=$row->body;?&gt;</p>
    
&lt;?php endif; ?&gt;

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.
#9

[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
$clean_title = str_replace(' ', '-', $orig_title);
$clean_title = strtolower($clean_title);

// Insert this and the post body into the db, (and probably an auto incrementing id)

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)
$clean_title = //whatever will give us the title

$title = str_replace('-', ' ', $clean_title);
$title = ucwords($title);

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.
#10

[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";
[/quote]
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 .




Theme © iAndrew 2016 - Forum software by © MyBB