2017-06-13 257 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] 

讓我知道在哪裏,讓你。