Special characters in mod_rewrite url

2019-08-08 19:34发布

问题:

I want this URL:

http://www.mydomainblabla.com/s/can+you+drill+shrinky+dinks?.html

to be rewritten to this one:

http://www.mydomainblabla.com/search.php?q=can+you+drill+shrinky+dinks?

I am using this mod_rewrite rule in my .htaccess to accomplish this

RewriteRule ^s/(.+).html$ search.php?q=$1 [L,QSA]

However, the result is not as I want it, when I go to the first url, I get a page not found message.

The same problem occurs when I visit this url:

http://www.mydomainblabla.com/s/http://www.zakgeldnodig.nl/.html

which should be rewritten into this one:

http://www.mydomainblabla.com/search.php?q=http://www.zakgeldnodig.nl/

What modifications should I make to my .htaccess to make this work?

回答1:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+s/(.+?)\.html [NC]
RewriteRule ^ search.php?q=%1 [L,NE]