Need help at redirect regex 301 - regex

i've moved an old site to wordpress and now i need to redirect the old links to new one
The old url
mysite.com/2010-11-02-11-05-12/category-old/subcategory/123-article
Here are my redirects:
to category page (working)
RedirectPermanent /2010-11-02-11-05-12/category-old/ /archive/category-new/
to article page (not working)
RedirectPermanent /2010-11-02-11-05-12/(.+?)/^[0-9]+-(.+?)/?$ /$1 [L,R=301]
redirect url (404)
mysite.com/archive/tag/subcategory/123-article
the url should look like this (the number before are removed 123-):
mysite.com/article
could anyone help?

Change it to:
RewriteEngine on
RedirectRule 2010-11-02-11-05-12/(.+?)/[0-9]+-(.+?)/?$ /$1 [L,R=301]
Which seems to be what you were trying to use. I removed the opening forward slash as they are not included in a match from .htaccess. I removed the caret (^) from before the numbers as that would stop it matching. Let me know any problems.

Related

HTACCESS : Redirect (301) thousands of Url's containing directories to simple url's

I need to convert with HTACCESS method tons of URL's allready produced (and already indexed...) by Wordpress for articles containing folders/subfolders to simples URL's without any folder/subfolder name.
Example:
FROM https://www.website.com/Animals/Cats/mycat.html TO https://www.website.com/mycat.html
FROM https://www.website.com/Animals/Dogs/mydog.html TO https://www.website.com/mydog.html
FROM https://www.website.com/Countries/France/bordeaux.html TO https://www.website.com/bordeaux.html
etc...
I already changed permalinks options in Wordpress config. So, now URL's produced are in the good format (Ex: https://www.website.com/bordeaux.html) without any folder name.
My problem is to redirect all OLD Url's to this new format to prevent 404 and preserve the rank.
If tryed to add in my .htacess this line :
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
I egally tryed RedirectMatch 301 (.*)\.html$ method and it's the same. I'm going crazy with this.
What am i doing wrong and could you help me?
Thanks
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
The URL-path matched by the RewriteRule pattern never starts with a slash. But you can use this to only match the last path-segment when there are more "folders". And the target URL also needs to end in .html (as per your examples).
So, this can be written in a single directive:
RewriteRule /([^/]+\.html)$ /$1 [R=301,L]
This handles any level of nested "folders". But does not match /foo.html (the target URL) in the document root (due to the slash prefix on the regex), so no redirect-loop.
(No need for any preceding conditions.)
Here the $1 backrefence includes the .html suffix.
Just match the last part of the url and pass it to the redirect:
RewriteRule /([^/]+)\.html$ /$1.html [R=301,L,NC]
It will match any number of directories like:
https://www.website.com/dir1/page1.html
https://www.website.com/dir1/dir2/page2.html
https://www.website.com/dir1/dir2/dir3/page3.html
https://www.website.com/dir1/dir2/dir3/dir3/page4.html

Regex redirect of wordpress blog from subdomain to domain

I am trying to redirect the WordPress blog post from old to new domain.
We had a blog on subdomain http://blog.domain.xyz/ and after the migration is on main domain https://www.domain.xyz/
On the old blog, URL of the blog post was:
http://blog.domain.xyz/2020/03/25/post-name
(part /2020/03/25/ is just an example of date)
now I need it to redirect to:
https://www.domain.xyz/post-name
I matched with regex domain and date part:
http\:\/\/blog.domain.xyz\/\d{4}\/\d{2}\/\d{2}\/
I know how to redirect manually all posts one by one, but there are more than 1000 posts, so this is not an option.
I can't figure out how to take post-name part and apply it to the new domain
I think you need something like the following.
RewriteRule ^/\d+/\d+/\d+/(.*)$ https://www.domain.xyz/$1 [R=301,L]
// ^/ start at the root
// \d+/\d+/\d+/ match date folders like 2020/03/25/
// (.*) the part we want to keep "some-slug"
// $ end of match
// $1 put the part we want to keep here "some-slug"
Basically matching any urls with /2020/03/25/post-name and redirects to https://www.domain.xyz/post-name.
Note: this assumes you're adding the redirect at the old domain.
In the meantime I figured it out:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.domain.xyz$
RewriteRule \d{4}/\d{2}/\d{2}(.*)$ https://www.domain.xyz$1 [R=301,L]
</IfModule>

htaccess redirect url with trailing %20

We use pretty urls on our site. I had an external technician add back links some years ago. He did a great job, but in one case, he consistently added a link with a trailing space character.
https://www.example.com/item/item/%20
This has been indexed as %20 and I can see on my back link reports that there are 87 sites that point to the URL with %20 at the end.
If I can redirect this, then my page /item/item/ would gain 87 back links.
We use rewrite rules, and I have tried every solution here on stack overflow, but none has worked. Some non working solutions are:
RewriteEngine on
RewriteRule ^(.*[^\ ])\ +$ /$1
RedirectRule (.*)\s$ $1 [R=301]
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2
I have tried a redirect 301 but these don't work either.
redirect 301 /item/item/%20 /item/item/
redirect 301 /item/item/+ /item/item/
Some things that helps - this is not a site wide pattern. It is just one particular URL that got propagated out into the world incorrectly. And it is not a space anywhere in the string - it is always at the end.
Thanks.
It would also work fine for me to convert the trailing %20 to a known character like a - because I could redirect it /item/item/- to item/item/
You can use this rule as your topmost rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^(.*)(?:\s|\x20)+$ /$1 [L,NE,R=301]

Creating a 301 redirect with regEx till forward slash

I'm rebuilding a website and changing a CMS. Former CMS had weird URL structure - it was adding post id in the end of the URL with forward slash. For example:
www.mydomain/category/someurl/54
I'd like to create 301 redirects in .htaccess with regex for a new CMS (WordPress). How could I create a pattern that would just simply match old CMS URL to new URL by removing last forward slash and id in URL?
Basically it should be like this:
301, from www.mydomain/category/someurl/54 to www.mydomain/category/someurl
You can place this rule as very first rule just below RewriteEngine line.
RewriteEngine On
RewriteRule ^(.+?)/[^/]+/?$ /$1 [L,R=301]

301 redirect strip QUERY_STRING

I am planning to relaunch a website soon. Part of the relaunch is a switchover from Joomla! to Wordpress.
Besides some old pages, which will be redirected with some exact expression, I have a lot of pages, which will be redirected to the new root page.
Whats the right regular expression, if I want to redirect URLs like these:
http://www.somedomain.com/index.php?somevar=123&com_option=somevalue
http://www.somedomain.com/index.php?com_option=someothervalue
All the URLs have in common:
http://www.somedomain.com/index.php?
+
com_option
How can I redirect all those pages with one line of redirect code in .htaccess?
I assume you do not need somevar and only want to carry over com_option
RewriteEngine on
RewriteBase /
RewriteRule index.php(.*)com_option=(.*)$ http://newsite.com/index.php?com_option=$2 [L,R=301]
You will need to do something like this:
RedirectMatch 301 (.*)/the/old/domain/and/url/(.*) http://the/new/domain/and/path/$2
Now, that the site is gone live, I see that none of the answers to my questions worked out.
After some trial an error I found a solution:
By now Google offers a live-demo:
1) Go to google & search for "site:sqlxpert.de".
2) Click on one off the links having "option" in the URL.
3) You will be redirected to the new pages root - which in this case is the same domain and also an index.php, but the d*** QUERY_STRING is gone.
I needed a .htaccess snippet, which does the following (expressed in words):
If requested file is
INDEX.PHP or INDEX2.PHP
AND
QUERY STRING contains "option"
then 301 REDIRECT to
http://www.sqlxpert.de/
WITHOUT any QUERY STRING
The solution:
RewriteCond %{QUERY_STRING} option
RewriteRule ^index\.php http://www.sqlxpert.de/$1? [L,R=301]
RewriteRule ^index2\.php http://www.sqlxpert.de/$1? [L,R=301]