rewrite htaccess to remove slug product - wordpress

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.

Related

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 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]

Redirect slug, replace plus for dashes

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.

.htaccess redirect URL last part to same URL query strings value?

In my wordpress site I need do do below redirect.
http://www.mydomain.com/magazine-landing/lon-mayf/ to the http://www.mydomain.com/magazine-landing/?code=lon-mayf
Here lon-mayf can be anything.
I've tried below code in .htaccess file after # END WordPress but it is not work.
RewriteEngine On
RewriteRule ^magazine-landing/*$ www.mydomain.com/magazine-landing/?magcode=$1 [R=301,L]
How could I do this redirect?
Thanks.
RewriteRule's are executed in the order they appear. The rule for wordpress matches all requests, so it will never reach your rule. Besides that, your rule doesn't really do what you describe it should do.
Place this rule above the wordpress rule and below RewriteEngine on:
RewriteRule ^magazine-landing/([^/]+)/?$ /magazine-landing/?code=$1 [QSA,R,L]
Replace the R flag with R=301 after you are done with testing to make it permanent. Testing with permanent redirects can have unexpected outcomes due to caching.

.htaccess 301 redirect from one subdirectory to another and preserve folder/directory structure

I've done a lot of searching on how to do this, but none of the solutions seem to be working with this specific problem of redirecting from one subdirectory to another while preserving the original folder/directory structure.
For example I would like to redirect all incoming deep links from:
example.com/post/123 to example.com/archive/post/123
Basically I just want to move my original Wordpress blog from the root directory to a subdirectory, but I have a few popular incoming links and I don't want them to break.
Would appreciate any help or if anyone has had similar experience moving blogs yet retaining their incoming links.
Thanks,
Josh
Please try this:
For each of your links write as follows in the .htaccess file
redirect 301 /OldSubdirectoy/old/old.htm http://www.domainname.com/NewSubdirectory/new/new/new.htm
Write in this structure for each of your popular links to keep SEO.
I figured this out a while ago properly and wrote an article about it on Medium:
Basically this is the code I needed in .htaccess in my root directory:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(p/.*)$ http://phocks.org/historic/$1 [R=301,L]
RewriteRule ^(contact.*)$ http://phocks.org/historic/$1 [R=301,L]
RewriteRule ^(about.*)$ http://phocks.org/historic/$1 [R=301,L]
RewriteRule ^(wp-content.*)$ http://phocks.org/historic/$1 [R=301,L]

Resources