htaccess干净的网址和替换空格和%20与 -

Sas*_*dar 3 .htaccess

我很努力地做这项工作.目前我的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>
Run Code Online (Sandbox Code Playgroud)

如果我尝试这个网址,一切都很有效,除了一件事:

http://www.domain.com/ test/
Run Code Online (Sandbox Code Playgroud)

浏览器将其转换为:http://www.domain.com/%20test/ 基本上在域之后如果路径以空格开头或%20则失败.任何人都可以指出一个解决方案,其中起始空间将被删除?

UPDATE

目标:

www.domain.com/   this is a      test      /   hello there     /
Run Code Online (Sandbox Code Playgroud)

要么

www.domain.com/   this is a      test      
Run Code Online (Sandbox Code Playgroud)

www.domain.com/this-is-a-test/ 要么 www.domain.com/this-is-a-test/hello-there

anu*_*ava 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]
Run Code Online (Sandbox Code Playgroud)

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