我覺得這樣的工具不得不發佈這個問題,但對於我的生活,我無法弄清楚如何解決我的問題。 (我也讀/試過以前的帖子,但沒有幫助我)Htaccess Mod Rewrite不工作的漂亮網址
我試圖把https://mywebsite.com/article.php?slug=pretty-url到https://mywebsite.com/article/pretty-url
我有是$_GET
法是不承認的塞的問題,所以它給了我一個404錯誤。 slu is肯定在我的數據庫中。我不知道爲什麼我無法檢索它。
以下是我的htaccess代碼和我的php代碼來調用頁面。
的.htaccess代碼:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule (.*) https://mywebsite.com/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [L]
ErrorDocument 404 /404.php
#Pretty URL for Blog
RewriteRule ^article/([0-9a-zA-Z]+) article.php?slug=$1
#Rewrite for certain files with .php extension
RewriteRule ^about$ about.php
RewriteRule ^services$ services.php
RewriteRule ^portfolio$ portfolio.php
RewriteRule ^blogs$ blogs.php
RewriteRule ^tutorials$ tutorials.php
RewriteRule ^contact$ contact.php
RewriteRule ^privacy-policy$ privacy-policy.php
RewriteRule ^terms-of-service$ terms-of-service.php
RewriteRule ^sitemap$ sitemap.php
</IfModule>
PHP代碼的article.php頁:
//Get the blog. Only blogs that are published
$slug = $_GET['slug'];
$publish = intval(1);
//Query the database
$sql = "SELECT * FROM blogs WHERE publish = $publish AND slug = $slug";
//Execute the query
$stmt = $db->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
嘗試將SQL更改爲'「SELECT * FROM blogs WHERE publish ='$ publish'AND slug ='$ slug'」' –
@CaelanGrgurovic好消息是我沒有獲得404,而是一個網頁瀏覽器中的網址,但不會從數據庫中提取任何內容。 – Soletwosole
我注意到,瀏覽器有時需要一段時間才能與htaccess文件一起生效。嘗試清除緩存或切換瀏覽器並在其上進行測試! :) –