WordPress by default creates ugly query-string fashioned URLs, sometimes with an ID present, like http://example.com/?p=30. These URLs are fully useful, however aren’t terribly friendly to look for search engines and website guests.
The .htaccess file is basically employed for making pretty permalinks and keyword injected URLs for your website. By enabling pretty permalinks WordPress creates URLs supported website content such as post and page titles, class and tag names, and dates for archives.
To alter permalinks visit the Settings -> Permalinks SubPanel on your WordPress Dashboard. Choose any permalink structure aside from Default and click on the Save Changes link.
Upon saving your changes, WordPress tries to make your default .htaccess file. If your root WordPress directory is writable by the server, the file is formed automatically. If WordPress is unable to create the .htaccess file, you’ll see directions on a way to manually produce the file.
Creating a permalink structure using the month and year like:
creates a permalink like this:
Click to expand the images.
Using permalinks offers several benefits, as represented within the following list:
➤ Search Engine Optimisation (SEO): Keywords in your URL may be a should for SEO. Search engines can use these keywords in their algorithm for positioning in their search results.
➤ Forward Compatibility: In spite of what platform your website uses (WordPress, Drupal, Joomla!), having a solid permalink structure may be easily replicated must you ever migrate.
➤ Usability: Visitor-unfriendly ID URLs create it equally unpleasant to share a link with a friend. It’s troublesome to differentiate the content between your ID driven URLs.
➤ Sharing: During this web era of social networking, sharing may be a natural extension of our on-line presence. Keywords within the URL would create finding your link extremely simple and convey an immediate context for the content.
.htaccess revising Rules
The ‘‘secret sauce’’ behind the WordPress permalink mechanism is summarized in 2 revising rules added to the .htaccess file after you alter permalinks:
RewriteCond % !-f
RewriteCond % !-d
Quite merely, these rules check the URL accustomed access your website to envision if it refers to an existing file or directory within the filesystem hierarchy. The !-f and !-d notations are negations; .htaccess is guaranteeing that the URL doesn’t visit any valid filesystem pathname.
If the URL will in truth match a legitimate file, for example, a WordPress body perform like wp-login.php, then the URL is left unchanged. If there’s no file or directory therewith name, the URL is handed to the WordPress core code to be converted into a question against the content information.
We’ll poke into the steps accustomed convert a URL string into a MySQL question during a bit a lot of detail.
The .htaccess file can even manage URL redirects. If you alter your About page from http://example.com/about to http://example.com/about-me, anyone who visits your original URL will hit a 404 page.
A URL redirect can send from the previous URL to the new URL therefore your guests won’t stray.
This additionally alerts search engines concerning the new URL so that they will update their index.
Following is an example of a 301 permanent redirect to a static page:
redirect 301 /about http://example.com/about-me
Configuration management through .htaccess
The .htaccess file is incredibly powerful and may management over simply URL structure. As an example, you can management PHP configuration choices using the .htaccess file. to extend the memory assigned to PHP use this command:
php_value memory_limit 64M
This will increase the memory limit in PHP to 64MB. You’ll additionally increase the max file size transfer and post size:
php_value upload_max_filesize 20M
php_value post_max_size 20M
Now the utmost file size you’ll post from a form and upload is ready to 20MB. Most hosting corporations set these values to around 2MB by default, therefore these are settings that may be used usually for larger file uploads. Not all hosting corporations can enable these values to be set in your .htaccess file, and could create a slip-up on your computer if that’s the case.
The .htaccess file can even be used for security functions. Using .htaccess permits you to limit access to your computer by IP address, primarily lockup it down from anonymous guests. To lock down your computer by IP addresses, add the subsequent code to your .htaccess file:
AuthName “Access Control”
deny from all
#IP address to whitelist
allow from xxx.xxx.xxx.xxx
Replace xxx.xxx.xxx.xxx with any IP address that you just need to grant access to your computer. You can have multiple ‘‘allow from’’ lines therefore add as several IP addresses as you would like. this can solely enable access to your computer if you’re victimization an IP address outlined here.
A a lot of wide used choice is to lock down your wp-admin directory. this implies solely IP addresses you specify will access your admin dashboard URLs. This makes it a lot of more durable for anyone else to undertake to hack your WordPress backend. To accomplish this produce a separate .htaccess move into your wp-admin directory with the preceding code.
Remember that the majority ISPs assign shopper addresses dynamically, therefore the IP address of the pc you are using can modify now and again. If you get locked out, simply update your .htaccess file together with your new IP address or delete the file altogether. This is often not a great tip if you permit open registrations on your website as a result of you would like to permit your users access to the wp-admin directory.
You can additionally alter error logging from the .htaccess file. The primary step is to make a php-errors.log file in your WordPress root directory. Then add the subsequent code to your .htaccess file to alter error logging:
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag log_errors on
php_value error_log /public_html/php-errors.log
This enables error logging, however suppresses any error messages from displaying. Once more this is often an ideal setup for a production surroundings as a result of you don’t need errors in public displayed.
The .maintenance File
WordPress incorporates a inherent maintenance mode that may be enabled by the .maintenance file. to check this feature merely produce a brand new .maintenance file and add the subsequent line of code:
<?php $upgrading = time(); ?>
Add this file to your WordPress root directory and your website can instantly enter maintenance mode. This locks down your website for all guests and displays a generic maintenance message. The time() function is replaced with any UNIX-formatted timestamp.
You can set a custom maintenance page by making a maintenance.php file and inserting it in your
wp-content directory. WordPress uses this file to show throughout any forced maintenance periods that you set. This permits you to make a custom maintenance notice to your website guests.
This file is additionally employed by the WordPress automatic upgrade method. A .maintenance file is formed right before WordPress installs the new core files throughout an upgrade. This ensures there are never any error messages for your guests throughout this method.
What am I missing here? Let me know in the comments and I’ll add it in!
Did you like this guide of setting up pretty permalinks for better SEO in WordPress? Share it with your friends on Facebook, Twitter, and Google Plus!
Don’t forget to Subscribe to our Newsletter for more insightful post updates, tips and guides!
See you in the next post.
Hi, I’m Aakanksha, a computer science engineer and the founder of TheTechPower. I help people make there online presence process easier by empowering them with clean, beautiful and easy to use wordpress themes.