1

This one has me stuck so I am hoping someone will be able to point me in right direction.

I have a database keeping all my posts which is called blog_posts_content. I then have a viewpost.php file which requests the post content matching the postSlug with a get request on the "id" as seen below:

<?php require($_SERVER['DOCUMENT_ROOT'] . '/includes/config.php');

$stmt = $db->prepare('SELECT * FROM blog_posts_content WHERE postSlug = :postSlug');
$stmt->execute(array(':postSlug' => $_GET['id']));
$row = $stmt->fetch();

//if post does not exists redirect user.
if($row['postID'] == ''){
    header('Location: ./');
    exit;
}

?>

Here is an example postSlug in the database ->

https://website.co.uk/testfolder/viewpost.php?type=services&category=webdesign&id=postname

I'm then trying to get this showing as

https://website.co.uk/testfolder/services/webdesign/postname

So far I have this htaccess file within my testfolder

RewriteEngine On
RewriteBase /testingfolder/

RewriteRule ^([^/]+)/([^/]+)/?$ catpost.php?category=$1&id=$2 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?type=$1&category=$2&id=$3 [QSA,L]

The testfolder includes the following files:

index viewpost catpost

So I am treating index.php as the home page ofcourse, the viewpost the request the post content and then catpost to show categories.

Now I hope I have detailed the background enough so now to the issue.

The above does not show the page so if im on index and click the link with the slug outlined above I get redirected back to the index page as per the php request.

If i just use this htaccess and just have the postSlug as the postname then I can get the post to show

RewriteEngine On
RewriteBase /testfolder/

RewriteRule ^category/(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]

However, with that my url ends up as

https://website/testfolder/postname

I'm questioning whether its the php request or the htaccess rules.

How do I solve this problem?

1 Answer 1

1

Having a router would simplify this. Here is how you can build one.

This will simplify what you are trying to accomplish. Most PHP frameworks, Wordpress, and other CMS's do something similar to this.

.htaccess

This rewrites all requests to your index.php.

<IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

From there you can route the requests to the correct file.

index.php

<?php

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $uri);

switch($segments[1]) {
    case "posts":
        // Do your posts stuff here.
        break;
    case "category":
        // Do your category stuff here.
        break;
}

If you use the URL of this page stackoverflow.com/posts/55977515/ we see the domain, posts, and 55977515 which is the ID for your post. So, instead of Stack Overflow using query parameters, they use URL segments to figure out what page we are requesting. Here is a simplified version of the process:

  1. They use a switch statement like above to figure out what type of page we want
  2. Okay, we want posts.
  3. Do they want all posts or a specific post?
  4. Okay, there is a post ID 55977515
  5. We need to get that post from the database and show it

In the example above, where it says Do your posts stuff here. that indicates where you will need to put your code to get the specific post.

Example router for you to play with. I suggest you create a file with just this in it, get that working, then slowly start adding more code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the suggestion and apologies on the long gap from your answer. I'm trying to understand your reply. I'm not sure what i should be placing between the switch function. Sorry if it seems obvious.
I've updated my answer to hopefully clarify what you will need to do. Please let me know if you still need help. And if you do, let me know what you've tried, and what didn't work. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.