2017-06-13 259 views
1

首先,我知道这个问题在Stack Overflow上出现很多。我一直在阅读许多其他帖子的答案,但是,没有任何答案解决了我的问题。我只会告诉你我现在有什么。.htaccess为博客重写不起作用

我有我写的自定义博客,所有文章都存储在数据库中。在数据库中,我有一个URL字段,其中存储了带连字符的文章的名称。例如:我先交

在列表中的文章页面上的更多按钮,我已经链接到页面以这种方式:

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

当你点击阅读更多按钮,它将带您到本页面:https://example.com/post.php?id=my-first-post。我希望做的是这样的一个网址:https://example.com/post/my-first-post

这里是我的重写规则至今:

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] 

目前,与我的.htaccess,当我点击我的链接,它没有。在此先感谢您的帮助!

回答

1

你在正确的道路上。尝试对您在URL中传递的字符串进行查询。

事情与此类似:

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] 

让我知道在哪里,让你。