问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
