大网数据广告活动
服务器运维问答

Apache RewriteMap with URLs containing space doesn't work

· 52CE 我爱测

I am actually using a RewriteMap directive inside my vhost to redirect a list of 800 URLs. It works quiet well: RewriteEngine On RewriteMap redirects dbm=db:/data/apps/project/current/configuration/etc/httpd/conf/redirects.db Rew…

问题原文

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

I am actually using a RewriteMap directive inside my vhost to redirect a list of 800 URLs. It works quiet well:

RewriteEngine On
RewriteMap redirects dbm=db:/data/apps/project/current/configuration/etc/httpd/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]

I use a redirect.txt file containing the mapping. Then it is converted to a db file:

httxt2dbm -f db -i /data/apps/project/current/configuration/etc/httpd/conf/redirects.txt -o /data/apps/project/current/configuration/etc/httpd/conf/redirects.db

For example for this kind of URL, it is OK:

/associations/old_index.php /

But when the URL contains spaces it doesn't work: (I suppose it will be the same with other special characters)

/Universités%20direct   /

How to handle this case?

已采纳解答

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

A workaround might be to internally rewrite URLs that contain a space to a hyphen (replace the space with a hyphen) and include the hyphenated URL in your rewrite map instead.

If you only have some URLs that contain a single space within the URL then you could use something like the following directive before your existing directives:

RewriteRule ^(.+)\s(.+)$ $1-$2

And then use the following in your rewrite map:

/Universités-direct /

UPDATE: If you have URLs that contain two spaces (eg. /the force awakens) and some with one space then you could add an additional rule:

RewriteRule ^(.+)\s(.+)\s(.+)$ $1-$2
RewriteRule ^(.+)\s(.+)$ $1-$2

These rules do assume that you don't have URLs that end with a space. And no URL has more than one contiguous space.

If three spaces then add another rule before the above...

RewriteRule ^(.+)\s(.+)\s(.+)\s(.+)$ $1-$2

I would tend to do it this with multiple (simple) rules, rather than a generic "convert everything in a single rule", unless you specifically need that. A generic rule will run recursively, reducing multiple spaces to a single character. You will also likely need additional flags (ie. DPI) to prevent a known rewrite bug in Apache.

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

资料来源与适用范围

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

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

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