问题
I have a rewrite question. It drives me crazy. I created a new website in worpdress. I want to redirect old urls (that are in google) to the new urls. That works fine except for the following urls (there is a plus in the old url)
www.domain.com/slugname/this+is+a+slug
Has to be rewritten to:
www.domain.com/slugname/this-is-a-slug
How to replace the plus for a dash (.htacces? add_rewrite_rule?)
Sombody has example code?
I tried .htacces an add_rewrite_rule in worpdress, but im not smart enough ;)
回答1:
If you're happy to do it on an individual basis per URL then the following in your .htaccess
file (it's important the file is spelt correctly) should work:
RewriteRule ^oldpage$ http://www.example.org/newpage? [R=301,L]
So your example might be:
RewriteRule ^slugname/this+is+a+slug$ http://www.example.org/slugname/this-is-a-slug? [R=301,L]
The R=301
part of the rule makes the redirect permanent, which I assume is the desired effect. Removing this would make the redirect a 302
, which is known as temporary.
If you are looking to replace all +
with -
in the URL then you can use a generic statement:
RewriteRule ^(.*)+(.*)$ /$1-$2 [L,R=301]
回答2:
There is a plugin in WordPress called Redirection, that will allow you to redirect old links to new links. It takes a lot of hassle from trying to do it in the .htaccess. You can use regex on the plugin. Once installed the plugin can be found under the tools menu.
来源:https://stackoverflow.com/questions/25120308/redirect-slug-replace-plus-for-dashes