-1

I am developing a website in core php with custom cms. I am facing problem while making the blog url's seo friendly.

My Url is of this type and I want the url like websitename/blog/title If I fix it through htaccess file, that will be static because i want a single function which works for future blog posts as well. So Please Help me to solve this issue.

isha
  • 1
  • 4

2 Answers2

1

Make the specific blog title a $_GET value with htaccess

RewriteRule    ^blog/([A-Za-z0-9-]+)/?$    /blogs.php?blog=$1    [NC,L]

then you can print the blogs out from the blog` id or title

Once that is set up you need to get the $_GET data from the blog table of the database in your blogpost.php script like so:

$sql ="SELECT * FROM blog_table WHERE title = ".$_GET['blog']." LIMIT 1";

$blog = mysqli_fetch_array(mysqli_query($sql));

echo '<h1>'.$blog['title'].'</h1>';
echo '<p>'.$blog['content'].'</p>';

obviously security needs to be thought about and prepared statements would be better.

fixdreamer
  • 101
  • 9
  • Well that works to make yoursite.com/blog/any-blog work and that's how its done on thousands of sites. What you now need to do is make your blogpost.php script do what it needs to. It should be taking the $_GET variable of the blog (probably its title) and retrieving it from the database and dispaying is as the blog. Is that what you are doing? it should be one page for all blogs and is 100% dynamic. – fixdreamer Aug 17 '15 at 13:02
  • Hi, I have tried this as well, but it is not working. Please tell me what i do now – isha Aug 18 '15 at 06:29
  • I am using echo $row['post_slug']; for showing. that's why slug is coming but when i put yours, its not showing. PLease help – isha Aug 18 '15 at 10:16
  • Ok well I have just visited that domain and it looks like you have the access file working to some extent because it now supports dynamic urls. If you just coppied my code for the htaccess then the $_GETvar I set (confusingly) was blog so try: echo $_GET['blog']; if that doesnt show whatever you wright then try: print_r($_GET); and then you'll know where you're going wrong. – fixdreamer Aug 18 '15 at 10:24
  • Please post the output to: print_r($_GET); – fixdreamer Aug 18 '15 at 11:53
  • ok are we talking about: http://kreativemines.com/blogpost.php is that the script you have put and still have print_r($_GET); on???? – fixdreamer Aug 18 '15 at 11:56
  • because I just ran it with this url: http://kreativemines.com/blogpost.php?e=four and it didn't show so the code isn't on there are you having FTP issues? – fixdreamer Aug 18 '15 at 11:58
  • well that code isn't on that script now is it? have you removed it? – fixdreamer Aug 18 '15 at 12:01
  • I can but I'm afraid I'm going out now so can't till tonight. Sorry – fixdreamer Aug 18 '15 at 12:22
1

You could try:-

RewriteEngine On
RewriteBase /

RewriteRule ^blogpost.php/(.+)(/?)$ /blogpost.php/?post_slug=$1 [NC,L]

# or slightly nicer would be to remove the `.php` thus:-
RewriteRule ^blogpost/(.+)(/?)$ /blogpost.php/?post_slug=$1 [NC,L]
Professor Abronsius
  • 26,348
  • 5
  • 26
  • 38