删除域后斜杠斜杠

2023-09-02 00:29:43 作者:觸摸不到的痛

这是我的的.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>

但是,当我去 ** ///// ,尾随斜线不会消失。我究竟做错了什么?

But, when I go to **/////, the trailing slashes will not go away. What am I doing wrong?

推荐答案

在%{REQUEST_URI}变量被降低了额外的斜线时,它得到prepped。 (*)(。*)://域,以便在条件的RewriteCond%{REQUEST_URI} ^ // $ 绝不会像 HTTP请求匹配,因为。组织//// ,在REQUEST_URI变量被减少到只有 / 。尝试使用THE_REQUEST变量:

The %{REQUEST_URI} variable gets reduced of extra slashes when it gets prepped. So the condition RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ will never match because for a request like http://domain.org////, the REQUEST_URI variable gets reduced to just /. Try using the THE_REQUEST variable:

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

此外,在preFIX(领先的斜杠)被扒掉请求URI时重写规则在htaccess文件,所以规则重写规则。 %1 /%2 [R = 301,L] 永远不会匹配,因为正则表达式的 至少需要一个字符相匹配。当URI是 / 和斜线被剥夺,这是用来在URL匹配是一个空字符串的URI。因此,使用 ^ 或(。*)或什么的的一切,包括什么相当于正则表达式需要使用。

Additionally, the prefix (the leading slash) gets stripped off of the request URI when rewrite rules are in an htaccess file, so the rule RewriteRule . %1/%2 [R=301,L] would never match because the regex . requires at least one character to match. When the URI is / and the leading slash gets stripped, the URI that's used to match in the url is a blank string. So using ^, or (.*), or something equivalent of "everything including nothing" regex needs to be used.