简体   繁体   中英

Apache htaccess RewriteRule to swap “+” for “-”

At the request of the SEO firm that was hired to review an old system, we need to change all the old URLs (examples below) from using "+" to "-". The system is dynamic and none of these links are stored, but they are worried about it looking like broken pages if the change is made programmatically if these articles/pages are posted somewhere. So they want to do it cleanly with 301s in the htaccess file.

Examples of old URLs:

domain.com/Articles/Charter+Day+in+PA/

domain.com/Things+To+Do/

domain.com/Search/Things+To+Do/

domain.com/things+to+do/Brandywine+River+Museum/

to:

domain.com/Articles/Charter-Day-in-PA/

domain.com/Things-To-Do/

domain.com/Search/Things-To-Do/

domain.com/things-to-do/Brandywine-River-Museum/

So, I placed the following in the htaccess file, but it doesn't appear to work:

RewriteEngine on

RewriteRule ^(.*?)\+(.*?[^\+]*?\+.*)$ $1-$2 [N]
RewriteRule ^(.*?)\+(.*?[^\+]*)$ $1-$2 [R=301]

RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Any help is appreciated!

You can use this code:

RewriteEngine on

RewriteRule "^([^+]*)\++([^+]*\+.*)$" /$1-$2 [L]
RewriteRule "^([^+]*)\+([^+]*)$" $1-$2 [L,R=301]

RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

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