regex SQL command for bulk URL update via wp-cli - sql

We made URL permalink changes to WordPress sites, removing a no longer needed date in URLs and keeping just the domain, subfolder and postname.
from /%year%/%monthnum%/%day%/%postname%/
to /%postname%/
While the 301 redirect is working perfectly we have hundreds of URL references in post contents that we'd like to update at once with WP-CLI. Does anyone know how to create a regular expression SQL command for this in WP-CLI to remove the date from URLs, making all internal links current again?

Related

Apache Rewrite Condition After Upgrade

We recently upgraded a system and have noticed that our URLs have changed. I would like to configure our Apache web server so that it permanently detects the old URL and redirects users to the new URL.
All URL parameters have remained unchanged apart from one, see below;
Old URLs;
https://example.com/products/cat_one/record/1656782?lang=eng&type=grey
https://example.com/products/cat_one/record/1188746?lang=eng&type=blue
https://example.com/products/cat_one/record/4499814?lang=eng&type=black
New URLs;
https://example.com/products/cat/record/1656782?lang=eng&type=grey
https://example.com/products/cat/record/1188746?lang=eng&type=blue
https://example.com/products/cat/record/4499814?lang=eng&type=black
As you can see, the cat_one parameter has changed to cat.
We've been told by the supplier that this kind of redirect isn't possible and it's our responsibility to update all old URLs manually, surely this isn't correct?
How can I achieve this, wither via htaccess, vhosts file, or similar - I've seen various ways to achieve this. I think I require a 301 redirect / rewrite rule?

htaccess redirect with query string preserved

What I'd like to happen via a .htaccess redirect is:
An url request of : "http://subdomain1.mysite.com/addToBasket.php?query_string"
is redirected to
"http://subdomainA.subdomain1.mysite.com/addToBasket.php?query_string"
The subdomains 'subdomain1' and 'subdomainA.subdomain1' are properly set up in DNS as fully qualified domain names
However, subdomain1.mysite.com exists on a different server than subdomainA.subdomain1.mysite.com - so absolute url format is required throughout.
In the request site subdomain directory the 'addToBasket.php' script file will not exist but will exist in the redirected to subdomain directory
The requested query string is a wildcard but must not be altered during the redirection.
I've searched and tried many of the example solutions - but the '?query string' part is always being omitted in the redirection.
Reason for the subdomain1 => subdomainA.subdomain1 madness and differing servers is that a new site is being launched on the new server with a fresh start DB but the old site generated basket related emails to customers containing links which they clicked to fulfil the order process - so the old site will still be hosted on the old server for a while but under a subdomain just to allow time for these email links still in the wild to work with the system they were designed for.
Unfortunately the new developers created a similarly named subdomain system as used in the old site - so the old site will now have a 'subdomain.subdomain' url format to allow it to have a different IP and function.
Any help very much appreciated!
Unless you explicitly overwrite the query string, it will be preserved automatically, see RewriteRule
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
This means, if you just redirect without adding a query string yourself, it will be passed on, e.g.
RewriteRule ^addToBasket.php$ http://subdomainA.subdomain1.mysite.com/addToBasket.php [L]
or if you want to redirect not just this one, but every possible request
RewriteRule ^ http://subdomainA.subdomain1.mysite.com%{REQUEST_URI} [L]

Use htaccess to remove any sub directories in URL

I run an Expression Engine site that will load an article page regardless of any made up sub directory names typed into the address bar.
For example all these addresses:
http://hellothere.example.com/health/mental/depression
http://cats.example.com/health/mental/depression
http://dogs.example.com/health/mental/depression
http://www.batman.example.com/health/mental/depression
http://www.1.2.3.4.5.6.example.com/health/mental/depression
Will load example.com/health/mental/depression
Obviously, this is less than ideal for SEO since I've got a potentially unlimited number of duplicate URLs.
I am trying to figure out how to use htaccess to strip anything before example.com and replace it with just www.
Any help would be appreciated!
You can not do this with htaccess. The way to do this is to set up a wildcard dns record so that all requests (*.example.com) are all directed to the same server.
See http://codex.wordpress.org/Configuring_Wildcard_Subdomains

Handling several thousand redirects with .htaccess

I am working on a site overhaul. As a result I am moving several pages over to a new format. They aren't keeping the same file name as before so the migration is a little tricky.
Example:
news.alpinezone.com/93467/ is becoming
http://alpinezone.com/still-more-skiing-and-riding-at-whiteface/
The news subdomain has accumulated in several years over 3000 articles. Is it OK to put 3000 + 301 redirects into an .htaccess file?
On a side note, for proper SEO, should I also make sure I use http:// instead of http:// www and also make sure they are fully lower case and also close with a / at the end of the URL. I am redesigning into wordpress and any combination pretty much works but I understand that for Google they can be considered unique but similar URL's so I want to stick with one as much as possible.
Thanks!
Apache does have some stuff for this, like RewriteMap or RewriteProg. I think htaccess files are read on every request, so I wouldn't want to make the size of it explode with 3000 lines of text - although I gut tells me it would handle it just fine. I think RewriteMap is only loaded once per server start or somethign like that, so thats a benefit.
But personally, I think I would just do an internal rewrite of any request to the news subdomain to a serverside script like php, and then inspect the uri, query the database to get the most current/up to date url slug for the id, and then do an external 301 redirect to the new url.
Have you considered mod_authnz_ldap to offload the authentication and authorization lookups to another server? I use this particular module on several enterprise servers with no problems whatsoever. It easily allows you to set up access to pages by group etc.

How do I rewrite URLs with Nginx admin / Apache / Wordpress

I have the following URL format:
www.example.com/members/admin/projects/?projectid=41
And I would like to rewrite them to the following format:
www.example.com/avits/projectname/
Project names do not have to be unique when a user creates them therefore I will be checking for an existing name and appending an integer to the end of the project name if a project of the same name already exists. e.g. example.project, example.project1, example.project2 etc.
I am happy setting up the GET request to query the database by project name however I am having huge problems setting up these pretty url's.
I am using Apache with Nginx Admin installed which mens that all static content is served via Nginx without the overhead of apache.
I am totally confused as to whether I should be employing an nginx rewrite rule in my nginx.conf file or standard rewrites in my .htaccess file.
To confuse matters further although this is a rather large custom appliction it is build on top of a wordpress backbone for easy blogging functionality meaning that I also have the built in wordpress rewrite module at my disposal.
I have tried all three methods with absolutely no success. I have read a lot on the matter but simply cannot seem to get anything to work. I am certain this is purely down to a complete lack of understanding on with regards to URL rewriting. Combined with the fact that I don't know which type of rewriting should be applicable in my case means that I am doing nothing more than going round in circles.
Can anyone clear up this matter for me and explain how to rewrite my URLs in the manner described above?
Many thanks.
If you are proxying all the non static file requests to Apache, do the rewrites there - you don't need to do anything on nginx as it will just pass the requests to the back end.
The problem with what you are proposing is that it's not actually a rewrite, a rewrite is taking the first URL and just changing it around or moving the user to another location.
What you need actually takes logic to extrapolate the project name from the project ID.
For example you can rewrite:
www.example.com/members/admin/projects/?projectid=41
To:
www.example.com/avits/41/
Fairly easily, but can you map that /41/ in your app code to change it to /projectname/ - because a URL rewrite can't do that.