2014-04-09 44 views
0

我已經添加以下代碼到我的htaccess文件中去除.php擴展名並向URL添加尾部斜槓。它運行良好,但它減少了我的頁面加載速度,我的CSS代碼根本沒有加載。我的所有網頁都很醜陋。刪除.php並使用htaccess不加載css添加尾部斜槓使用htaccess不加載css

請幫助

RewriteEngine On 
RewriteBase/

## hide .php extension snippet 

# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule^%1/ [R,L] 

# add a trailing slash  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule . %{REQUEST_URI}/ [L,R=301] 

# To internally forward /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 

回答

3

我們可以重寫使用.htaccess文件的URL。但在寫入.htaccess文件之前,我們需要驗證我們正在使用哪種服務器。您可以通過在php頁面中添加一個

<?php phpinfo(); ?> 

來輕鬆檢查您的服務器。

確保在Apache服務器中啓用mod_rewrite模塊。這可以通過檢查包含phpinfo的頁面來識別。

用於URL重寫常用的方法是如下

RewriteEngine敘述

這是強制性的任何Apache服務器。這用於啓用重寫引擎。在某些情況下,它應該是由default.So確保下面的代碼必須是強制性的在你的htaccess文件的頂部

RewriteEngine On 

重寫規則

在某些情況下

您可能只有幾頁,你可以在.htaccess文件中指定網站中的每個頁面。在這種情況下,你可以使用這些類型的重寫。例如

RewriteRule ^article/used/to/be/here.php$ /article/now/lives/here/ [R=301,L] 

假設你有重寫URL這樣的

http://en.wikipedia.org/wiki/url_rewriting

你應該寫東西像這樣在你的.htaccess文件

RewriteEngine On 
RewriteRule ^wiki/(.+)$ w/index.php?title=$1 [L] 
But actual implementation of the page will be like these 

http://en.wikipedia.org/w/index.php?title=url_rewriting

RewriteCond

這可以用於重寫某些條件相關的url。假設你需要添加或與您的網站名稱中刪除www和在一定條件下重定向到某個頁面,如「鏈接不可用,或錯誤消息」

例子:

RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC] 

您還可以參考這裏

Introduction to url rewriting

Rewrite with examples

這些是你有沒有加載CSS問題文件

樣式表文件可能會被對待而不考慮其擴展名,它不會加載爲css。包括在HTML頁面文件

例如,當你可以做以下的修復就可以了

  • 使用完整路徑

    <link href="css/style.css" rel="stylesheet" type="text/css" /> 
    

    <link href="http://sitename.com/css/style.css" rel="stylesheet" type="text/css" /> 
    
    • 移動css t o項目/公共目錄(如

    • project/public/css)使用絕對路徑到您的css,如 /css/index.css。確保外部文件的URL不被服務器重寫。

    另一種解決方案是嘗試這些方法太

    RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC] 
    
+0

這不是我的問題,先生。我只是問,htaccess工作正常,但添加上面的htaccess代碼後,我的css文件不起作用 – Alex

+0

@Alex:我在底部添加了答案請嘗試這些 –

+0

感謝它的工作 – Alex

1

試試這個代碼:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/$ $1.php 
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteRule (.*)$ /$1/ [R=301,L] 
相關問題