简体   繁体   中英

Remove + (plus symbol) from url using .htaccess or PHP

I've recently made some changes to my website's .htaccess file. I have a search form that searches a database using a simple php script.

Right now if a user searches for "red trucks" from mysite.com/search.php my website returns mysite.com/red+trucks.php (I've already set the .htaccess file to create friendly urls and to 301 redirect). I'm wondering how I remove the +plus symbols and change them to - hyphens?

My .htaccess file looks like this with your change put in CMate

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^search/(.*)\.php search.php?pro_query=$1
RewriteRule ^search/(.*)\.php$ /scripts/search.php?pro_query=$1
RewriteCond %{QUERY_STRING} ^pro_query=([0-9,a-z,.*,%25,%2B,%+]+)$
RewriteRule ^scripts/search\.php /search/%1.php? [R=301,NC,L]
RewriteRule ^([^+]*)\+(.*) $1\-$2 [N]

Try this rule if it is fine to replace all + symbols to - :

RewriteRule ^([^+]*)\+(.*) $1\-$2 [N]

In your case add it as the last rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM