Redirecting All Subdirectories to Single Location - wordpress

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.

Related

301 redirect if filename end has different characters

The filenames that I want to redirect are like following:
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-3-110.pdf
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-1-510.pdf
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-2-116.pdf
etc.
I want to 301 redirect all that contains the unchanged URL part /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022- to a file: /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022a.pdf
I tried this in htaccess without any success:
RewriteRule ^(.+/)DIDUFKU-BLINGSY9-bbteew-hhh-2022-(.*) /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022a.pdf [R=301,L]
Those files are deleted, those were duplicate files I decided to redirect but not redirecting. I put that rule in htaccess file in WP directory
If those files are deleted and you put this rule at the end of the .htaccess file, after the WordPress code block then it won't do anything since the request will be routed through WordPress and mostly likely trigger a (WordPress generated) 404 Not Found response.
This rule needs to go near the top of the .htaccess file, before the WordPress code block. ie. Before the # BEGIN WordPress comment marker.
However, your rule can be improved (to avoid unnecessary repetition), for example:
RewriteRule ^(wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022)-[\d-]+\.pdf$ /$1a.pdf
The $1 backreference contains the captured group from the RewriteRule pattern, ie. the string wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022.
You should test first with a 302 (temporary) redirect to avoid potential caching issues.

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.

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

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.

needed a suggestion in redirecting old URLs to a new URL

Earlier i have installed my code in the main domain itself. for instance,
www.abcd.com/xyz/page.html
there are about 300 pages indexed by google with the old URL.
Now i have removed that code from the main domain and installed the code in Sub-Domain. So it make all the URLs indexed by Google and all the backlinks are pointing to the old URL.
Now i need to point all the old invalid URL to new Valid URL when Users clicks on the old URL.
Please suggest how to handle this.
Thanks a lot...
If you are using Apache, I suggest that you use mod_rewrite as it allows to send the HTTP status 301 Moved Permanently, informing search engines that the contents of your website has been moved to a new location (rather than just deleted).
In a .htaccess file:
RewriteEngine on
RewriteRule (.*) http://newdomain.mysite.com/$1 [R=301,L]
The first line enables URL "rewriting", the fancy term for redirections of all kind. The second line is decomposed like this:
RewriteRule is the directive to match URLs and change the place they point to;
(.*) is a regular expression matching any requested file path (without an initial slash)
http://newdomain.mysite.com/$1 is the place where you want to send your visitors, and $1 is expanded to the previously matched path
[R=301,L] tells Apache to send the 301 Moved Permanently HTTP status code, and that it's the last RewriteRule that can match this request (it's useful only when you have multiple RewriteRules but it doesn't hurt to have it anyways).
The next time crawlers visit your site, they will notice the HTTP status and update their links to your new address. You should have it set up as soon as possible before Google thinks your whole site went 404.
If you are using Apache as your web server you can use mod_rewrite or mod_alias command. This is more a server job and not a programming job.
You can put the following in a .htaccess file or in the vhost container.
mod_rewrite
RewriteEngine On
RewriteRule ^xyz/page.html$ new-page.php [L,NC,R=301]
See: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
mod_alias
Redirect permanent xyz/page.html http://domain.com/new-page.php
See: http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
PHP
You can use a Location header to perform a redirection:
header('Location: http://domain.com/new-page.php', true, 301);
Java
public void HttpServletResponse.setHeader(String name, String value)
See: http://docstore.mik.ua/orelly/java-ent/servlet/ch05_06.htm
This should be handled by the server, preferably using an htacces redirect
PHP:
header('Location: new.url.com');

Resources