Jump to content
Larry Ullman's Book Forums

Recommended Posts

Back again... I'm putting this in the advanced section. I'm not sure if this is covered in the new book (I have the previous edition) or not.

 

Say you have a blog page with a few articles. The user clicks on an article and goes to a page with the complete blog post. The title of the page includes the query string for the article, like details.php?article_id=2. How would you make the title of the individual blog post page something like this-is-the-title-of-the-blog-post instead of details.php?article_id=2?

 

Thanks a bunch for anyone who can clue me in. :)

 

Link to comment
Share on other sites

Guest Deleted

What I would personally do, is just have the article_id coming in from the URL and nothing else. I would have that page use the article_id to query the database and have the database give it all the details: the full post, the title, and whatever other information you want. I wouldn't want to rely on this information coming from the URL because then anybody could tamper with it. I guess you could prevent this by validating all input, but just getting the data from the database seems easier.

 

I hope this helps :)

Link to comment
Share on other sites

What I would personally do, is just have the article_id coming in from the URL and nothing else. I would have that page use the article_id to query the database and have the database give it all the details: the full post, the title, and whatever other information you want. I wouldn't want to rely on this information coming from the URL because then anybody could tamper with it. I guess you could prevent this by validating all input, but just getting the data from the database seems easier.

 

I hope this helps :)

 

I don't think this answered my question. How do I make the URL the name of the post isntead of article_id=2? When you select an article from the main blog page, you're taken to a details.php page that displays the blog post you selected. I want that page's URL to be the name of the post and NOT article_id=2

Link to comment
Share on other sites

Guest Deleted

Oh! You want slugs. Ok. I've never used them so my answer might not be the best but if your URLs are going to be like this: http://www.mysite.com/details.php?title=title-goes-here

 

Maybe you could use str_replace() to get rid of the hypens and then query the database and tell it to pull up the row where the title matches this one? (Don't forget to validate and do stuff to prevent SQL injection.) I don't know if you'd run into case sensitivity problems, though.

 

There might be a better solution, though.

 

Maybe mod_rewrite would be better. I literally just learned about it yesterday so I am not the best source to help you with this, but yes, the book talks about mod_rewrite. In fact, that's where I read about it.

 

Hopefully this answer was more useful than the last one. I'm trying hard :)

Link to comment
Share on other sites

Guest Deleted

The 3rd edition of PHP Advanced and Object-Oriented Programming talks about mod_rewrite. It does so in Chapter 2. It starts on page 67. I have the book right here  ;)

Link to comment
Share on other sites

Does it cover changing a page from yoursite.com/blog.php?article_id=2 to yoursite.com/title-of-blog-post/ ? I'm looking all over google and every explanation is confusing and not what I'm looking for.

Link to comment
Share on other sites

Whoops. That's totally my bad. I looked at the forum title, and for some reason, I thought that this was the forum for the regular PHP/MySQL book, not the advanced book.

That's totally my bad.

 

Anyway, I have the 3rd edition of the advanced book, and yes, it definitely talks about mod_rewrite, as Buttercream said.

Terribly sorry about the confusion.

I gotta read more carefully next time.

Link to comment
Share on other sites

I'll definitely have to check out the book then. It's just so confusing, but it is an advanced topic. I know how to make clean URLs by putting an index page inside each directory, but using the blog post for the page title has alluded me this far. Amazing how every book tells you most of what you need, but there are several important things always left out that you NEED to make a complete site.

Link to comment
Share on other sites

Lou, yes, it does, but it's not quite that simple. Instead of changing:

yoursite.com/blog.php?article_id=2

 

To:

yoursite.com/title-of-blog-post/

 

You'd more likely change it to something like the following:

yoursite.com/2/title-of-blog-post/

 

That's a lot more flexible, and requires only one rewrite rule for mod_rewrite.

Link to comment
Share on other sites

Guest Deleted

I'm trying to figure this out for you and looking at the book for ideas. What I've been able to gather, the general process of using mod_rewrite is this:

 

<IfModule mod_rewrite.c>

RewriteEngine on

 

RewriteRule matchgoeshere rewritegoeshere

</IfModule>

 

It looks like you'll use a regular expression for your match and then a variable for your rewrite.

 

Somebody might come along and correct me but I'm going to go out on a limb and say your rule will be something like this:

 

RewriteRule ^details/[0-9]+/?$ details.php?article_id=$1

 

I think your URLs would look like this: http://www.sitename.com/details/2/post-title-here

 

The way I'm understanding this is it will look at the number that comes after details/ and put it where $1 is

Link to comment
Share on other sites

Buttercream, I think you're close, but there's a problem with your regex. You have to account for the readable title that comes after the ID.

There are a number of ways you can do this, but probably the easiest is to just get rid of the $ character at the end.

Also, you can just use the metacharacter \d to refer to [0-9].

Given all that, I'd do the following:

RewriteRule ^(\d+) blog.php?article_id=$1
Link to comment
Share on other sites

Still confused. I only got up to making sure the rewrite engine is on and redirecting to my index page when a user goes to a page that doesn't exist.

 

I'm using this site by the way.

 

http://blog.webilix.com/2012/10/implement-seo-friendly-urls-with-mod_rewrite/

 

I'm using xampp by the way, not ZendServer anymore. ZendServer was okay but I couldn't test any SSL with it.

Link to comment
Share on other sites

Sorry for jumping ahead too much there.

Let's imagine that you have the following links in your markup:

<a href="www.domain-name.com/1/why-nacho-chips-are-good">Why nacho chips are good</a>

<a href="www.domain-name.com/2/what-movie-will-Tarantino-do-next">What movie will Tarantino do next?</a>

<a href="www.domain-name.com/3/how-late-is-too-late-to-go-to-bed">How late is too late to go to bed?</a>

I think we can agree that the URLs being used about are very readable and ideal given the actual link text and title of the pages they're linking to.

With that said, you have to use mod_rewrite to turn those links into actual links that your script can process.

 

The first step is to check for the existence of the module as follows:

 

<IfModule mod_rewrite.c>
</IfModule>

 

The next step is to actually turn on the mod_rewrite engine as follows:

 

<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>

 

And after that, you need to write the actual regex that will convert the pretty URLs to URLs that your script can process. Using your URL examples above, you could do something like the following:

 

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\d+) blog.php?article_id=$1
</IfModule>

 

Which would give you the following three URLs based on the links created in the markup above:

blog.php?article_id=1
blog.php?article_id=2
blog.php?article_id=3

Does that all make sense?

Link to comment
Share on other sites

I don't get it.

 

1. If you're dynamically pulling say the first 5 blog posts from the database into your blog.php page, how would the links already be the titles of the blog posts?

 

2 One of the links on the main blog page would be say.... details.php?article.id=2. You click the link and you're on the details.php page and the query string pulls all the info from the database for the article requested

 

3 When working with mod rewrites, would the links on the main blog page still be say article_id=2, then when you click the link, the mod_rewrite rules take effect and you're redirected to a different link that's the title of your blog post?

 

4 I just don't see where the link would get the title of the blog post

 

I don't even know the right question to ask right now. :)

Link to comment
Share on other sites

I can already process clean URLs. You just set an index page inside the folder and in your controller use include depending on the action taken and you can maintain a clean URL at all times, say yoursite.com/register INSTEAD of register.php.

Link to comment
Share on other sites

Guest Deleted

HartleySan: I think you're going to need to adjust your regex so it won't get confused by the year in Lou's URLs: http://blog.webilix.com/2012/10/implement-seo-friendly-urls-with-mod_rewrite/ See the 2012? We don't want it to get confused over that. We want it to know to go for what's between the year and the title. However, I suck at regex so I won't even attempt to suggest a better one, lol.

Lou: You're going to use mod_rewrite. You're going to make a rewrite rule. This rule will be able to look at a URL like this one http://blog.webilix.com/2012/10/implement-seo-friendly-urls-with-mod_rewrite/ find the article_id, and pull up the page http://blog.webilix.com/2012/details.php?article_id=10 but without it ever showing that link in the URL.

So details.php will still be getting the artile_id via get but you won't see it happening in the URL.

 

Hopefully this makes a little more sense now :)

Link to comment
Share on other sites

Hehe... I'm going to study this more tomorrow. I am tired and nothing is making sense. I'm sure we can figure it out. And I will buy the new edition of the book soon as well.

Link to comment
Share on other sites

Guest Deleted

Maybe tomorrow we can come up with some code that you can just copy and paste into your .htaccess Then you could play with it and see it in action for yourself :)

Link to comment
Share on other sites

mod_rewrites are also covered in the Effortless Ecommerce book. For some reason, it took me awhile to master writing clean urls but I have managed it on my site. Have a look and see if that is what you require. Here's the code from my .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /

RewriteRule ^archive/([A-Za-z0-9-]+)/?$ archive.php?a=$1
RewriteRule ^blog/([A-Za-z0-9\+\-]+)/([0-9]+)$ blog.php?t=$1&id=$2
RewriteRule ^blog/$ blog.php
ErrorDocument 404 /404.php
</IfModule>

Be sure you update the links as well or they won't work.

Link to comment
Share on other sites

I can already process clean URLs. You just set an index page inside the folder and in your controller use include depending on the action taken and you can maintain a clean URL at all times, say yoursite.com/register INSTEAD of register.php.

This is fine, except for the fact that you have to make a new folder for every page, which can be rather tiring and hard to maintain as your site grows. I mean, imagine having to create a new folder every time you write a new blog post.

 

1. If you're dynamically pulling say the first 5 blog posts from the database into your blog.php page, how would the links already be the titles of the blog posts?

You have to pull the titles of the blog posts with the IDs. For example:

SELECT id, title FROM blog_posts;

And then in your PHP, you might do something like:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
  
  echo '<a href="http://www.domain-name.com/' . $row['id'] . '/' . str_replace(' ', '-', strtolower($row['title'])) . '/">' . $row['title'] . '</a>';
  
}

3 When working with mod rewrites, would the links on the main blog page still be say article_id=2, then when you click the link, the mod_rewrite rules take effect and you're redirected to a different link that's the title of your blog post?

No, it's the other way around. The links that you would see in your markup would be the human-readable ones, and those links would be converted to links like "?article_id=2".

 

4 I just don't see where the link would get the title of the blog post

You don't care about the title of the blog post. It's merely there to help people and SEO. All you care about is the number that represents the ID. The rest you can just ignore and not worry about processing.

 

To Buttercream:

 

HartleySan: I think you're going to need to adjust your regex so it won't get confused by the year in Lou's URLs: http://blog.webilix....th-mod_rewrite/ See the 2012? We don't want it to get confused over that. We want it to know to go for what's between the year and the title. However, I suck at regex so I won't even attempt to suggest a better one, lol.

Lou did not state that he wanted to use a year in his URLs. If he does though, then yes, the regex I suggested will have to be amended. This all depends on what Lou wants though.

Link to comment
Share on other sites

Guest Deleted

Lou did not state that he wanted to use a year in his URLs. If he does though, then yes, the regex I suggested will have to be amended. This all depends on what Lou wants though.

I thought Lou meant the site in the link belonged to him* I just now visited it and it seems like it was just something he* was referencing. My bad.

 

*Him, right? I don't see a gender listed in Lou's profile, so I assumed based on the name. (I knew a guy named Lou once.)

 

- - - -

 

I am also wondering something. Since the title is only in the URL for the benefit of search engines, does it even have to be unique?

Link to comment
Share on other sites

mod_rewrites are also covered in the Effortless Ecommerce book. For some reason, it took me awhile to master writing clean urls but I have managed it on my site. Have a look and see if that is what you require. Here's the code from my .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /

RewriteRule ^archive/([A-Za-z0-9-]+)/?$ archive.php?a=$1
RewriteRule ^blog/([A-Za-z0-9\+\-]+)/([0-9]+)$ blog.php?t=$1&id=$2
RewriteRule ^blog/$ blog.php
ErrorDocument 404 /404.php
</IfModule>

Be sure you update the links as well or they won't work.

 

I don't see how this RewriteRule works. I'd like the title of the blog post to be part of the URL of the blog post, I don't want the URL to be article_id=2. It looks like these rules make sure the URL is in the format I don't want. Can you explain each line?

Link to comment
Share on other sites

 Share

×
×
  • Create New...