3

我正在努力完成这项工作。目前我的 htaccess 包含以下代码:

#Debugging - Error reporting
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

#Commpression
<ifmodule mod_deflate.c="">
    <filesmatch ".(js|css|html|png|jpg|jpeg|swf|bmp|gif|tiff|ico|eot|svg|ttf|woff|pdf)$"="">
        SetOutputFilter DEFLATE
    </filesmatch>
</ifmodule>

Options All -Indexes +FollowSymLinks -MultiViews

<IfModule mod_rewrite.c>
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

    #RewriteCond %{THE_REQUEST} (\s|%20)
    RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
    RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

    #RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|bmp|gif|css|js)$ [NC]
    RewriteRule ^([^/]+/?.+)$ /index.php?req=$1 [L,QSA]

</IfModule>

如果我尝试这个 url,除了 1 件事之外,一切都很好,例如:

http://www.domain.com/ test/

浏览器将其翻译为:http://www.domain.com/%20test/ 基本上在域之后,如果路径以空格或 %20 开头,则失败。谁能指出将删除起始空格的解决方案?

更新

目标:

www.domain.com/   this is a      test      /   hello there     /

或者

www.domain.com/   this is a      test      

www.domain.com/this-is-a-test/或者www.domain.com/this-is-a-test/hello-there

4

2 回答 2

10

我为两年多前编写该代码而感到内疚:P

这段代码可以大大简化这一点:

# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]

# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]

# replace spaces by - in between
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]

PS:必须补充一点,您还需要修复这些 URL 的来源,因为获取这样的 URL 确实不正常。

于 2013-09-21T17:20:09.213 回答
0

这很好用

<IfModule pagespeed_module>
    ModPagespeed on
    ModPagespeedEnableFilters collapse_whitespace,remove_comments
</IfModule>
于 2021-03-29T14:02:38.300 回答