Redirect slug, replace plus for dashes

邮差的信 提交于 2019-12-25 02:39:06

问题


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

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