在.htaccess中用一个不间断的空格重写一个url

时间:2014-09-16 09:54:46

标签: regex apache .htaccess mod-rewrite

我有以下网址(有一些像这样):

mysite.co.uk/productdetails/408/20/Casio%C2%A0CTK1100/

我想将其重写为:

mysite.co.uk/Casio-CTK1100-in-Black/393

问题是这个有趣的%C2%A0 现在我有其他多个空格的链接,它们被重写如下:

# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]

# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=302,NE] (is just and example of a url with more than 1 space)

这样可以完成,但是当url只有一个空格时,就像上面显示的那样,它将无法工作。我需要在我的.htaccess文件中保留上述规则,以便能够重写具有多个空格的URL。

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

你可以这样:

# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]

# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]

RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=302,NE]

RewriteRule "^productdetails/408/20/(\S+?)/?$" /$1-in-Black/393 [L,R=302,NE]