0

I've created a blog from scratch to test my PHP/HTML/CSS/Jquery skills and such. Everythings going well and now I want to set up the htaccess file so that when a user reads a blog post the URL reads

http://www.mysite.com/blog/post-title-here

instead of

http://www.mysite.com/blog.php?id=30

Is this possible?

The blog setup is that the posts are held within a database. The post page is generated based on the ID of the blog post. The PHP retrieves the entire post via the ID and shoves it all into an object. So the blog titles is $blogpost->title;

3
  • Hello, That is possible to regenerate your URL with the use of htaccess file. You just write a rules for the new URL's that you want to generate Apr 25, 2014 at 9:44
  • I don't think that will be possible in htaccess since it doens't know what is your ID/title relation. Unless you have a lost of all blog posts ["id"]=>["title"] then i'm not sure how would htaccess rewrite the URL? Apr 25, 2014 at 9:45
  • That is not possible with htaccess the closest it could get you is http://www.mysite.com/blog/30. You will need to use php to do what you want.
    – Howli
    Apr 25, 2014 at 9:48

2 Answers 2

0

You must first make sure you have the mod modrewrite enabled on your site.

Second, the id of the post will have to be in the url to be able to load the post.

For example, if you want to rewrite

http://www.mysite.com/blog/30/post-title-here

to

http://www.mysite.com/blog.php?id=30

You can put the following code in your .htaccess:

RewriteEngine on
RewriteRule ^blog/(\d+)/.*$ blog.php?id=$1 [L]
1
  • Thanks a lot for this bit. That works close to what I was looking for. Is there anything PHP wise to change the URL or is it essential to have the post ID in there. The only reason I ask is because my wordpress site is setup so that each of the blog posts is wordpressisshit.com/hey-its-a-post The main reason I'm doing this is for SEO so would /blog/20/itsapost be acceptable the same as /blog/itsapost
    – BITmixit
    Apr 25, 2014 at 10:06
0

If your PHP script retrieves the post based on its ID, then you'll have to include it in the URL. You could try this:

http://www.mysite.com/blog/30/post-title-here

and then modify your .htaccess file to read the ID parameter like so:

RewriteEngine on
RewriteRule ^blog/([0-9]+)/([a-zA-Z-_])$ blog.php?id=$1 [L]

Or you can add new field in your posts table, called permalink or whatever and use it to retrieve the post, instead of the ID. Like so:

SELECT * FROM posts WHERE permalink LIKE 'post-title-here'
2
  • Is this worth doing for SEO or will SEO not care about numbers being used as a directory? I know SEO hates variables but what about directories? Also thanks, this is insanely useful :)
    – BITmixit
    Apr 25, 2014 at 11:24
  • @BITmixit I've always considered the canonical urls www.site.com/like-this-one to be more human readable as one can actually get a glimpse of what's behind the link by simply reading its slug. I can't really say what's better from a SEO perspective, but there are many other (much) more important things to do within the site & the pages in order to optimize them better and get higher rankings. Check here What makes a “friendly URL”? Apr 25, 2014 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.