Reputation: 110
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?
Upvotes: 1
Views: 832
Reputation: 62
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.
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.
<?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:
posts
.55977515
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.
Upvotes: 1