Share this page:
Wordpress URLs can be quite cryptic and hard to remember. For example, the URL of the sample post in a fresh Wordpress installation is http://192.168.1.50/?p=1, and the URL of the sample page is http://192.168.1.50/?page_id=2. These permalinks can be formatted to make them more human readable using Wordpress's permalink settings so that the sample page's URL is http://192.168.1.50/sample-page/ and the sample post's URL is http://192.168.1.50/hello-world/. These modified URLs are often referred to as pretty links.
Mod re-write must be enabled in Apache so that pretty links can be used. You can do that by typing this command in a terminal:
Apache's configuration must be modified by adding a set of directives that tell Apache how to rewrite URLs for Wordpress. When Apache receives a request with a pretty URL, it must redirect the request to Wordpress's index.php using mod rewrite. When you enable pretty links, Wordpress generates code containing rewrite rules that configures Apache to do this redirection. Login to the Wordpress dashboard, and go to the Permalink page in the Settings menu. Select the format you want, and click on the save changes button.
The rewrite rules will appear at the bottom of the page:
These are the rewrite rules that were generated with the settings I selected:
Note that Wordpress displays a message saying that it would write the .htaccess file if it was writeable. For security reasons, it's best not to allow Apache to modify .htaccess files. It's safer to update the file yourself.
The rewrite rules can be stored in two different places: your site's Apache virtual host file, or an .htaccess file.
If you enable .htaccess files, then Apache checks to see if a .htaccess file is available every time it receives a request. If there is a .htaccess file, Apache must read the rules that it contains and then parse them. The .htaccess file must be read on every single request handled by the server, which isn't a much of an overhead when a single page is served, but becomes a significant overhead on busy sites.
You may need to use .htaccess files if you don't have access to the virtual host file, but it's best to avoid using them if possible. If you can access your server's virtual host file, it's best to save the rewrite code there instead of in .htaccess files. When the rewrite code is placed in the virtual host file, it will be loaded just once when Apache starts up.
To save the code in your virtual host file, open it in an editor with this command:
Save the code generated by Wordpress in the Directory directive that defines /var/www:
Type control-o and press return to save changes. Type control-x to exit nano.
If you don't want to put the code in your virtual file, you can put it in a .htaccess file instead. The virtual host file still needs to be edited to enable the use of .htaccess files. If you can access the virtual host, then open it in nano as described above, and look for the section with the Directory directive that defines /var/www. Change the line
The complete virtual host file should look something like this:
If you're using shared hosting, you probably won't have access to the virtual host file, and AllowOverride should already be set to All (if not, talk to your hosting provider).
Now create the .htaccess file in the directory where Wordpress was installed (if you followed this guide on setting up Wordpress on Apache, this will be /var/www/):
Save the configuration code in the .htaccess file. Type control-o and press return to save changes, and type control-x to exit nano. Make the .htaccess file readable by apache:
Whether you saved the code in a .htaccess file or in a virtual host file, you need to restart Apache:
Now try browsing your site. You should see user friendly URLs in your browser's address bar.