Rewrite a url with one non-breaking space in .htaccess

浪子不回头ぞ 提交于 2019-12-13 19:17:12

问题


I have the following url (there are a few like these):

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

and I want to rewrite it to:

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

The problem is this funny %C2%A0 now I have other links with multiple spaces in there and they are rewritten as follows:

# 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)

This works prefect, however when the url only has one space in it like the one shown above it will not work. I will need to keep the above rule in my .htaccess file to be able to rewrite the urls with multiple space.

Any help welcome


回答1:


You can have it like this:

# 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]


来源:https://stackoverflow.com/questions/25865713/rewrite-a-url-with-one-non-breaking-space-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!