Rewrite a URL to include an extra parameter with a space in it

﹥>﹥吖頭↗ 提交于 2019-12-12 03:53:42

问题


Now that .htaccess rewrites all URLs and includes a parameter, how can that parameter have a space in it?

For example

  • http://old.io --> http://new.com?pa=v%20a

While %20 works when testing in a .htaccess simulator, it doesn't work on my DreamHost account.

  • http://new.com?pa=v%20a --> http://new.com?pa=v0a
  • http://new.com?pa=v\ a --> http://new.com?pa=v a
  • http://new.com?pa=v%2520a --> http://new.com?pa=v520a
  • adding the NE flag doesn't change anything

回答1:


You're right; Even though adding %20 works in the simulator, it does not work on a real httpd server.

To add a space, you need to escape it with a \. Here's what your htaccess file looks like:

RewriteEngine on
RewriteBase /

# Check if the host matches old.io,
# You might want to add www\. to that.
RewriteCond %{HTTP_HOST} ^old\.io

# Rewrite URLs with empty querystring
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://new.com/$1?pa=v\ a [L,R]

# Rewrite URLs with non-empty querystring    
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)$ http://new.com/$1?pa=v\ a&%{QUERY_STRING} [L,R]

Notice pa=v\ a in RewriteRule directives.



来源:https://stackoverflow.com/questions/41397961/rewrite-a-url-to-include-an-extra-parameter-with-a-space-in-it

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