Redirect slug, replace plus for dashes - wordpress

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 ;)

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]

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.

Related

rewrite htaccess to remove slug product

I have some URLs like this:
https://example.com/product-name
I'll like to have .htaccess convert to SEO URLs in this way:
https://example.com/product/product-name
or otherwise.
This is how my .htaccess currently looks (it is placed in my root directory):
RewriteEngine On
RewriteRule ^([^/]+)$ product/$1 [L]
I've read and tried a lot of similar examples during the last 24 hours to no avail. I'm sure my server allows rewriting. Hope someone can help.
Maybe try something like the following at the top of your .htaccess file, before the existing WordPress directives. ie. Before the # BEGIN WordPress comment marker.
RewriteRule ^([\w-]+)$ /product/$1 [R=302,L]
This is a "redirect", not a "rewrite". Since you said this was for SEO.
Ultimately, this may need to be a 301 (permanent) redirect, but test thoroughly first with a 302 (temporary) redirect to avoid potential cching issues.
The regex ^([\w-]+)$ is also more restrictive than in your example. Specifically, it excludes dots, so won't match actual files like index.php, but will still match product-name.

Weird Domain handling for Wordpress

I have a really weird issue. My customer wants to have a website structure that ist like the following:
www.mydomain.com/myapplication/de/
www.mydomain.com/myapplication/at/
www.mydomain.com/myapplication/ch/
They have installed Wordpress to the /de/ folder.
Now they want to also reach the same wordpress-installation by going to /at/ and /ch/ but keeping this URL-Fomat.
so
www.mydomain.com/myapplication/de/a-wordpress-page.php
should also be reachable like:
www.mydomain.com/myapplication/at/a-wordpress-page.php
www.mydomain.com/myapplication/ch/a-wordpress-page.php
Unfortunately putting the wordpress-application to the maindomain is not an option, due to their weird system...
I hope is understandable.
Hope someone can help me.
It's easy.
You can redirect or rewrite your /myapplication/at/ and /myapplication/ch/ URL paths to the main /myapplication/de/ using a RewriteRule in htaccess.
RewriteEngine on
RewriteRule ^myapplication/(at|ch)/(.*)$ /myapplication/de/$2 [L,R]
Put this at the top of your WordPress htaccess file.
And change R to R=301 when you are sure the redirection is working perfectly fine.
If you want to keep the URL format then just remove the R flag from the rule. The rule with R will make an invisible redirection of URLs.

Redirecting All Subdirectories to Single Location

I am using .htaccess to rewrite my subdirectories or additional URL information in links to a single location. I'm not quite sure how to phrase this, so I'll include examples below.
OLD URL NEW URL
oldsite.com/shop newsite.com/shop
oldsite.com/shop/something newsite.com/shop
oldsite.com/shop/category/product/123.html newsite.com/shop
oldsite.com/shop/landingpage newsite.com/shop
There are still a number of additional functions that oldsite is performing, so I only want the /shop (and anything after that) to redirect to the newsite.
If it would be easier to do this somewhere besides .htaccess that would be great too.
That can easily be done with mod_rewrite:
RewriteEngine On
RewriteRule ^shop https://newsite.com/shop [R=301,L]
This will look at the URL and, if it begins with "shop" (the leading / is ignored automatically), redirect to the new URL.
Note that it will also redirect /shopping. If that is not what you want, you'd have to modifiy the pattern.
It will use a 301 Moved Permanently HTTP status code, but you could change that to another 3XX code by modifying the R flag.

.htaccess 301 redirects wordpress

I'm trying top redirect a standard Wordpress "folder" URL of a page to a new one on the same domain while keeping all subfolders, query strings etc. From domain/old-path/ to domain/new-path/.
Now I have this:
RedirectMatch 301 ^/old-path(.*) /new-path$1
It works, but domain/old-path redirects to domain/new-path and, this being WP, another redirect happens from domain/new-path to domain/new-path/.
I would like both domain/old-path and domain/old-path/ to redirect straight to domain/new-path/ with the ending /.
All this while, like I said, keeping all "subfolders", query strings etc.
...this being WP, another redirect happens from example.com/new-path to example.com/new-path/.
If /new-path is a physical directory then it's not WP that is triggering the redirect, it is Apache's mod_dir that is "fixing" the URL.
However, since you are using WordPress, it is preferable to use mod_rewrite (RewriteRule) to perform the redirect instead of a mod_alias RedirectMatch in order to avoid conflicts. Different modules execute at different times during the request and WP already uses mod_rewrite.
Try something like the following at the top of your .htaccess file before any existing WP directives:
RewriteRule ^dir/?(.*) /new-dir/$1 [R,L]
As you can see, although the slash is optional in the RewriteRule pattern, this is never captured by the regex and the slash is always present in the substitution.
Change the R (temporary) redirect to a R=301 only when you are sure it's working OK.

.htaccess Rewrite rule to add word in URL in a specific group of URLs

I created some posts on my WordPress website but after some time I changed their URLs. The problem is that Google is still indexing the old ones so I would need a redirect while the change is not all done.
I only need this in a group of URLs.
What I need is to redirect all URLs starting with:
http://www.telesgotos.pt/inspecao-video- + the requested location (for example: telesgotos.pt/inspecao-video-lisboa)
to
http://www.telesgotos.pt/inspecao-de-video- + the requested location (same example: telesgotos.pt/inspecao-de-video-lisboa)
I've searched several similar questions but I don't know .htaccess syntax at all so I don't know how to target the first part of the URL, add the needed word and append the rest of the URL...
Thank you in advance!
Regards,
Ricardo
In your /.htaccess file, insert the following just below RewriteEngine on:
RewriteRule ^inspecao-video-lisboa/?$ /inspecao-de-video-lisboa [R=301,L]
If you don't want to be specific, then use this instead:
RewriteRule ^inspecao-video-(\w+)/?$ /inspecao-de-video-$1 [R=301,L]

Resources