【问题标题】:Remove trailing slash after domain删除域后的斜杠
【发布时间】:2012-01-02 21:14:00
【问题描述】:

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

但是,当我转到**///// 时,尾部的斜线不会消失。我做错了什么?

【问题讨论】:

  • 当我去那里时,我得到一个 403... 还有,你的 index.html 存储在哪里?顺便说一句,我去那里时没有斜线。
  • 我禁止其他人访问,对此感到抱歉。
  • 我只能说,从我看到的情况来看,它看起来不错,但我不知道您是否不允许访问页面。

标签: apache .htaccess redirect apache2


【解决方案1】:

%{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]

此外,当重写规则位于 htaccess 文件中时,前缀(前导斜杠)会从请求 URI 中去除,因此规则 RewriteRule . %1/%2 [R=301,L] 永远不会匹配,因为正则表达式 . 需要 at至少要匹配一个字符。当 URI 是 / 并且前导斜杠被剥离时,用于匹配 url 的 URI 是一个空白字符串。因此,需要使用 ^(.*) 或等效于“包括任何内容在内的所有内容”的正则表达式。

【讨论】:

    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多