gpt4 book ai didi

regex - htaccess clean urls & 用 - 替换空格和 %20

转载 作者:行者123 更新时间:2023-12-01 08:31:33 30 4
gpt4 key购买 nike

我正在努力完成这项工作。目前我的 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>

如果我尝试这个网址,除了 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

最佳答案

我对两年多前编写该代码感到内疚: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:必须补充一点,您还需要修复这些网址的来源,因为获得这样的网址确实不正常。

关于regex - htaccess clean urls & 用 - 替换空格和 %20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18935257/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com