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.
Related
I was using translatepress on my WordPress site so my site URLs were like example.com/en-ae/hair-transplant but now I have removed translatepress so now my URLs are like example.com/hair-transplant but I have submitted URLs for SEO with the en-ae slug. I want that if en-ae is present in any URL then it gets removed automatically and gets redirected to page without en-ae.
For example example.com/en-ae/hair-transplant redirects to example.com/hair-transplant.
You need the rewrite module of Apache: mod_rewrite.
Then in your htaccess this:
RewriteEngine on
RewriteRule ^en-ae/(.*)$ $1
To remove the /en-ae prefix from all requested URLs (to help preserve SEO), you would need to add the following near the top of the root .htaccess file, before the WordPress code block (ie. before the # BEGIN WordPress comment marker):
# Remove "/en-ae/" prefix from all requests
RewriteRule ^en-ae/(.*) /$1 [R=301,L]
You do not need to repeat the RewriteEngine directive, which already occurs later in the file (in the WordPress code block).
The R=301 flag triggers an external "permanent" redirect - without which the URL-prefix is not actually removed. However, you should first test with a 302 (temporary) redirect to avoid potential caching issues.
The slash prefix on the substitution string is necessary to avoid a malformed redirect, if the RewriteBase directive is omitted from the WordPress code block.
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.
I have one domain that used to have its own WP installation, say www.aaa.example.
Now the content of this has been included in a different domain in a sub-page, say bbb.example/aaa-subpage/.
Also, the old domain www.aaa.example has a DNS A record pointing to the WP Server of bbb.example.
What I want now is, that all calls go to www.aaa.example/* should be redirected to bbb.example/aaa-subpage/ no matter which sub-page on aaa.example was used. So everything should go to one sub-page in the new domain.
I have tried some 301 redirect plugins but so far no chance... all calls from aaa.example simply go to the top-level page of bbb.example.
To redirect requests to aaa.example/<anything> to bbb.example/aaa-subpage/ you would need to add the following mod_rewrite directives to the top of the root .htaccess file. Crucially, it must go before the WordPress front-controller (ie. before the # BEGIN WordPress comment marker).
# Redirect "aaa.example/<anything>" to "bbb.example/aaa-subpage/"
RewriteCond %{HTTP_HOST} ^(www\.)?aaa\.example [NC]
RewriteRule ^ https://bbb.example/aaa-subpage/ [R=301,L]
You do not need to repeat the RewriteEngine directive that should already appear later in the file (in the WordPress section).
Test first with a 302 (temporary) redirect to avoid caching issues.
You will need to clear your browser cache before testing.
Try this line in the .htaccess:
RedirectMatch ^/$ /aaa-subpage/
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.
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');