htaccess 在重定向中用(转义的)破折号中断 URL

htaccess breaks with (an escaped) dash in the redirect URL

我有一组 URL,格式为 www.website.co。uk/businessname

这些是为了执行 htaccess 代理重定向以从服务器上的另一个域提取数据。

这是我的:

#Start Business pages
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputside.org.uk/swellcottage$
RewriteRule ^(.*) http://www.datasite.co.uk/ [P]

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn-books$
RewriteRule ^(.*) http://www.datasite.co.uk/ [P]
#End Business Pages

第一条规则完美运行,在 URL www.outputsite.org.uk/swellcottage 上显示 http://www.datasite.co.uk/swellcottage 的内容,但第二条 rule/condition 不起作用。

我看着转义 URL 中的 - 字符:

  RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn\-books$
    RewriteRule ^(.*) http://www.datasite.co.uk/ [P]
    #End Business Pages

但这似乎仍然没有正确参与,重写规则不起作用,并且该站点回复后备 404 页面 www.outputsite.org.uk/hawthorn-books 不存在。

我还应该在哪里以及如何转义破折号,否则我的语法有什么问题?干杯

ps。我知道文件名 (index.php) 和文件夹名 (/places/) 不受代理重定向的约束。

更新

我的 htaccess 中有更多规则:

#to add www. to each request. 
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

#Start biz pages
...
#End biz Pages

#to prevent error looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

我也有 运行 与上面相同的代码 hawthornbooks 没有破折号,转义或其他代码并且代码工作正常,所以我需要找出转义破折号的正确方法.所有代码和工作都是 UTF-8。

注意:此问题与 Why if I put a "-" dash in a rule in my htaccess doesn't work? 类似,但我找不到解决该问题的方法。

这是一个临时而非永久的解决方案,但您可以尝试以下规则:

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn([^\/]*)\/?$
RewriteRule ^(.*) http://www.datasite.co.uk/ [P]

这将查找任何符合模式 www.outputsite.org.uk/hawthorn 后跟 / 的任意数量的字符(或零个字符)的 URI ] 然后以(可选)/.

结尾

如果问题只是一个连字符,这应该有效:

#Start Business pages
RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(swellcottage)/?$ http://www.datasite.co.uk/ [P,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(hawthorn.books)/?$ http://www.datasite.co.uk/ [P,NC]
#End Business Pages