大网数据广告活动
网站运营与故障问答

How to eliminate space on .htaccess

· 52CE 我爱测

Somehow a space was introduced in some of my URL like: /marques-voiture/Ford /marques-voiture/ Ford I attempted to correct that with the following rule on .htaccess RewriteCond %{REQUEST_URI} ^/marques-voiture/\s.*$ [NC] RewriteC…

问题原文

原帖链接 · BernardA · CC BY-SA 3.0

Somehow a space was introduced in some of my URL like:

  • /marques-voiture/Ford

  • /marques-voiture/ Ford

I attempted to correct that with the following rule on .htaccess

RewriteCond %{REQUEST_URI} ^/marques-voiture/\s.*$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \/marques-voiture\/\s(.*) https://www.example.com/marques-voiture/$1 [R=301,L]

Regexp's seem to be correct according to regexp101.com, but .htaccess is solemnly ignoring that.

I do have a number of other rules on .htaccess, but I do not see a conflict there.

Any ideas?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

RewriteRule \/marques-voiture\/\s(.*) https://www.example.com/marques-voiture/$1 [R=301,L]

Your problem is probably the slash prefix on the RewriteRule pattern. In per-directory .htaccess files the directory-prefix is first removed from the URL-path that is matched. The directory-prefix always ends with a slash, so the URL-path that is matched never starts with a slash. So, the above rule never matches, not because of the space, but because of the slash prefix!

So, try the following instead:

RewriteRule ^marques-voiture/\s(.*) https://www.example.com/marques-voiture/$1 [R=301,L]

You also don't need the first RewriteCond directive that checks against the REQUEST_URI - this is doing the same thing as the RewriteRule pattern.

And you don't need to escape slashes in most Apache regex (there are no delimiters).

Make sure you clear your browser cache, as any (erroneous) 301s will have been cached by the browser.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。