2012-01-02 98 views
4

後,這是我的.htaccess文件:刪除斜線域

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    # Remove multiple slashes anywhere in URL 
    RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ 
    RewriteRule . %1/%2 [R=301,L] 

    # Never use www prefix! 
    RewriteCond %{HTTP_HOST} ^www.domain\.org [NC] 
    RewriteRule (.*) http://domain.org/$1 [R=301,L] 

    # Remove multiple slashes after domain 
    RewriteRule ^/(.*)$ http://domain.org/$1 [R=301,L] 

    # Remove trailing slash in some cases 
    RewriteRule ^(.*)\.css/$ http://domain.org/$1.css [L,R=301] 
    RewriteRule ^(.*)\.js/$ http://domain.org/$1.js [L,R=301] 
    RewriteRule ^(.*)\.jpg/$ http://domain.org/$1.jpg [L,R=301] 
    RewriteRule ^(.*)\.jpeg/$ http://domain.org/$1.jpeg [L,R=301] 
    RewriteRule ^(.*)\.png/$ http://domain.org/$1.png [L,R=301] 
    RewriteRule ^(.*)\.gif/$ http://domain.org/$1.gif [L,R=301] 
    RewriteRule ^(.*)\.xml/$ http://domain.org/$1.xml [L,R=301] 

    # Force trailing slash 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_URI} !index.php 
    RewriteCond %{REQUEST_URI} !(.*)\.css 
    RewriteCond %{REQUEST_URI} !(.*)\.js 
    RewriteCond %{REQUEST_URI} !(.*)\.jpg 
    RewriteCond %{REQUEST_URI} !(.*)\.jpeg 
    RewriteCond %{REQUEST_URI} !(.*)\.png 
    RewriteCond %{REQUEST_URI} !(.*)\.gif 
    RewriteCond %{REQUEST_URI} !(.*)\.xml 
    RewriteCond %{REQUEST_URI} !(.*)/$ 
    RewriteRule ^(.*)$ http://mydomain.org/$1/ [L,R=301] 

    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

    # MIME types 
    AddType text/css .css 
    AddType text/javascript .js 

    # Enable compression 
    AddOutputFilterByType DEFLATE text/html text/plain text/css  text/javascript text/x-css text/x-javascript text/x-js text/htm application/x-javascript application/javascript application/js application/x-js image/png image/gif image/jpg image/jpeg 

    #Skip browsers with known problems 
    BrowserMatch ^Mozilla/4 gzip-only-text/html 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 

    php_flag display_errors on 
</IfModule> 

<IfModule !mod_rewrite.c> 
    ErrorDocument 404 /index.php 
</IfModule> 

但是,當我去**/////,尾隨斜線不會消失。我究竟做錯了什麼?

+0

當我去那裏時,我得到了一個403 ...還有,你的'index.html'存儲在哪裏?順便說一下,當我去那裏時沒有斜線。 – nmagerko 2012-01-02 21:15:54

+0

我禁用了其他人的訪問權限,對此抱歉。 – 2012-01-02 21:18:25

+0

我只能說,它看起來很好,但我不知道你是否不允許訪問頁面。 – nmagerko 2012-01-02 21:21:09

回答

5

%{REQUEST_URI}變量在準備好時會減少額外的斜線。所以條件RewriteCond %{REQUEST_URI} ^(.*)//(.*)$永遠不會匹配,因爲對於像http://domain.org////這樣的請求,REQUEST_URI變量被減少到僅僅是/。嘗試使用THE_REQUEST變量:

RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ (.*)//([^\ ]*) 
RewriteRule^%2/%3 [R=301,L] 

此外,前綴(領導斜線)被剝去請求URI時重寫規則在htaccess文件,所以規則RewriteRule . %1/%2 [R=301,L]永遠不會匹配,因爲正則表達式需要至少一個字符匹配。當URI爲/且前導斜槓被剝離時,用於在url中匹配的URI是空白字符串。因此需要使用^(。*),或者等同於「一切都不包含任何東西」的東西。