How to Update WordPress Permalinks with WP-CLI on LAMP Server

Permalinks (short for “permanent links”) are the URLs that point to specific pages or posts on your website. In the context of WordPress, permalinks refer to the structure of the URLs for your individual posts, pages, and other types of content on your WordPress site.

WordPress allows you to customize the permalink structure to make URLs more user-friendly and search engine-friendly. By default, WordPress uses query strings like ?p=1234 which is neither user-friendly nor seo-friendly. This numeric type of permalink structure is unofficially referred to as “ugly” permalinks. Changing the permalink structure to something more meaningful, e.g. postname is a fairly straightforward process. All you need to do is go to the admin dashboard of your WordPress installation, navigate to Settings => Permalinks, change the structure accordingly and click “save”. However, the aim of this tutorial is to change the permalinks programmatically, with the help of wp-cli.


WP-CLI (WordPress Command Line Interface) is a command-line tool for managing WordPress installations. It allows developers, system administrators, and power users to interact with WordPress directly from the command line, rather than relying on the admin dashboard. WP-CLI can help automate repetitive and bulky tasks like deleting thousands of spam comments, updating plugins and themes, creating and managing users and others. This can make a huge difference when you work on huge amounts of data, since the WordPress admin dashboard cannot allow for such level of automation. For example, from the web interface, the maximum amount of comments you can remove is 20 at a time. With wp-cli you don’t have any restrictions!

For the purpose of this tutorial, I will assume that you use an Apache web server that has wp-cli already installed and you have ssh access to your web server. If you are running a self-managed VPS server, follow the official WordPress documentation on how to install wp-cli. If you are on a shared host that does not support wp-cli, consider updating to a hosting provider that fully support wp-cli like HostArmada.

Update WordPress Permalinks with WP-CLI

To change the WordPress permalinks in wp-cli, run the below command in the terminal:

wp rewrite structure '/%postname%/'

The above code will change the permalink structure to postname and it will attempt to flush the permalinks, which works in the same way as clicking “Save” in the admin dashboard. However, if you have a LAMP setup, this will not be enough. The Apache web server needs an update in .htaccess file too. To update the file, you need to flush the permalinks and also add support for Apache’s mod rewirte in wp-cli config file:

wp rewrite flush --hard

The above command updates the permalinks and attempts to update the .htaccess file. However, for allowing this to work, you also need to update the global wp-cli config.yml file. It is typically located in /root/.wp-cli folder. If there is no such file, create it with:

nano config.yml

Finally, add the below configuration in the wp-cli configuration file:

  - mod_rewrite

Save the file (with nano, you can do ctrl + X and then press “y”). Now, if you run the above wp-cli commands again, the permalinks will be saved and updated.