這是你的.htaccess
應該是什麼樣子:
RewriteEngine On
# Remove www.
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule^http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
# Remove file extensions, add a trailing slash.
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]
This大約是往返的URL刪除的文件擴展名一個很好的參考文章。請記住,爲此,您必須在所有鏈接中引用非擴展版本,例如<a href="about">About</a>
,而不是<a href="about.php">About</a>
當你在做.htaccess
的事情,我也可以建議添加下面的代碼片段。前兩個關注網站速度,第二個關注自定義404頁面,第三個關注強制UTF-8
(因此您不必在HTML中聲明它)。
# Expires caching (Caching static files for longer drastically improves performance, you might even want to put even more aggressive times)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
# Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# 404 Page
ErrorDocument 404 /404.php
# Force UTF-8
AddDefaultCharset utf-8
I wrote about this在CodePen博客文章,如果你有興趣。
HTML BP有一個瘋狂的700+行的.htaccess,你可以看到一些很酷的技巧。
來源
2014-10-20 22:39:04
Tim
您確定這些網址仍然可以這樣工作嗎?看起來你想將用戶從'../ page.php'重定向到'../ page /',但Apache會知道在哪裏尋找'../ page /'? – Rudie 2014-10-20 22:44:18