簡體   English   中英

.htaccess加載index.php文件以獲取友好的URL

[英].htaccess to load index.php file for friendly url

我有這個網址:

www.mywebsite.com/news/best-ever-phones

best-ever-phones是可變的,可以是任何東西。
index.php文件正在使用url best-ever-phones檢查數據庫中的文章

問題是在./news/.htaccess文件中我有以下代碼:

RewriteEngine On
RewriteBase /news/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]

當我訪問www.mywebsite.com/news/best-ever-phones網址時,應該加載index.php文件,但服務器返回錯誤404 Not found

怎么了?

嘗試這個

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule   ^(.+)$   index.php?article=$1   [L]

我認為問題可能出在重寫基礎和您的相對路徑上。 但這就是我要做的。

<IfModule mod_rewrite.c>

   Options -Indexes

   RewriteEngine On
   RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1  [NC]

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d

   RewriteRule ^([^\.]+)$ $1.php [NC,L]
  </IfModule>

這應該可以解決問題,將索引重命名為news.php

也許試一下:

RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]

這是假定您希望/字符出現在$article變量中,因此,如果要在/news/index.php ,可以在邏輯上分隔友好URI的不同部分。 否則,單段友好的URI(例如/news-item-1或諸如此類)可以以斜杠結尾或不以斜杠結尾。

確實,您不希望使用斜杠,因為使用相同的URL加載同一頁面可能會導致重復的內容罰款(/計數),因此您可能無法匹配\\/? 捕獲組外部或301中的index.php重定向到URL,並刪除了斜杠。 這樣,您還可以獲得干凈的查詢參數,例如/news/something-big?orly=yes而不是/news/something-big/?orly=yes

我正在使用此站點來測試重寫。 否則未經測試,因為我們沒有您的代碼,因此無法判斷它是否在您的環境中有效。

當我們單擊任何URL時,將使用該代碼隱藏其擴展名。 但是,如果要轉到index.php頁面,請編寫

echo '<a href="index.php">Index Page </a>';

  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM