Matthew Davis
Matthew Davis

Reputation: 117

.htaccess rewrite for a blog is not working

First, I know this question appears a lot on Stack Overflow. I've been reading the answer's from many of other post's, however, none of the answers have fixed my problem. I will just show you what I have at this point.

I have a custom blog I wrote, and all the articles are stored in the db. In the database, I have a URL field, which stores the name of the article with hyphens in it. Example: my-first-post

On the list articles pages, on the read more button, I have linked to the page in this manner:

<a href="post.php?id=my-fist-post">read more</a>

When you click on the read more button, it takes you to this page: https://example.com/post.php?id=my-first-post. What I would like to do is a URL like this: https://example.com/post/my-first-post

Here is my rewrite rules so far:

Options +FollowSymLinks -MultiViews -Indexes
irectoryIndex index.php index.html index.htm
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)/?$ /post.php?id=$1 [L]

Currently, with that in my .htaccess, when I click on my link, it does nothing. Thanks in advance for your help!

Upvotes: 1

Views: 42

Answers (1)

M David
M David

Reputation: 127

Your on the right path. Try doing a query on the string you pass in the URL.

Something similar to this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^&\s]+)$
RewriteRule ^(?:post\.php|)$ /%1? [R=301,L]

RewriteCond %{QUERY_STRING} ^post=([^&\s]+)&id=([^&\s]+)$
RewriteRule ^(?:post\.php|)$ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ post.php?id=$1&r [L,QSA]

Let me know where that gets you.

Upvotes: 1

Related Questions