URL 中包含空格的 %2520(双倍空格)

发布于 2025-01-05 20:53:00 字数 730 浏览 1 评论 0原文



我的 URL 结构就像

http://www.example.com/folder/index.php?dir=dir1

为了能够从第一个 URL 访问它

http://www.example.com/folder/dir1

并同时将第一个 URL 重定向到第二个 URL,我的 htaccess (在“文件夹”中)是

Options +FollowSymLinks 

RewriteEngine On
RewriteBase /folder

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]

RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

问题是,如果 URL 中存在任何目录名称包含“空格”,URL 显示 %2520 而不是“空格”。

请建议修改 htaccess,使其显示 %20 或者最好是简单的“空格”?

My URL structure is like

http://www.example.com/folder/index.php?dir=dir1

To be able to access it from

http://www.example.com/folder/dir1

and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is

Options +FollowSymLinks 

RewriteEngine On
RewriteBase /folder

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]

RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.

Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-01-12 20:53:01

尝试在重定向上添加NE,即

 RewriteRule ^ %1? [L,R=301,NE]

编辑

既然您已经阅读了我的 htaccess,您是否认为有可能进一步缩短它

以下是一些评论

Options +FollowSymLinks 

RewriteEngine On
RewriteBase /folder

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]

#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e 
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

try adding a NE on the redirect ie

 RewriteRule ^ %1? [L,R=301,NE]

EDIT

Since you've read my htaccess, do you see any possiblity of shortening it further

Below are a couple of comments

Options +FollowSymLinks 

RewriteEngine On
RewriteBase /folder

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]

#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e 
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文