1
我在我的服務器上有html靜態文件。我想訪問規則 -使用.htaccess的URL重寫幫助
- 如果用戶訪問http://example.com/test/他應該得到的文件http://example.com/test.html
- 的內容。如果用戶訪問http://example.com/test(沒有尾隨斜線),他被轉到http://example.com/test/(運行規則1並獲取文件test.html的內容012)
- 規則1和2只應在文件test.html存在時纔會觸發。
到目前爲止,我有 -
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine On
RewriteBase/
# If it's a request to index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\ [NC]
# Remove it.
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]
# Add missing trailing slashes to directories if a matching .html does not exist.
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# If it's a request from a browser, not an internal request by Apache/mod_rewrite.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# And the request has a HTML extension. Redirect to remove it.
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)/?$ $1.html [QSA,L]
</IfModule>
雖然它適用於http://example.com/test,它失敗了http://example.com/test/(500內部錯誤)
不應在倒數第二行採取尾隨斜槓的護理?
[更新] 如果我用濃湯建議(以下的.htaccess),我得到兩個http://example.com/test和http://example.com/test/ 404(用斜線)
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]
</IfModule>
[更新2] 我已經放棄了。下面的代碼可以工作,但不是強制以/(http://example.com/test/)結尾,而是刪除最後一個斜槓(http://example.com/test)。在光明的一面,它確保內容僅由一個網址指向,保留SEO。我現在要和它一起生活。
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
# And there is no trailing slash, rewrite to add the .html extesion.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
</IfModule>
對不起,對於http://example.com/test和http://example.com/test/ – pravin 2010-10-15 10:53:12
404 @pravin:哦,它固定。 – Gumbo 2010-10-15 11:04:08
對不起,仍然404s – pravin 2010-10-15 11:20:23