Tag Archives: .htaccess

.htaccess Files – How to make one and adjust it for WordPress



Yesterday I decided to work out some SEO (Search Engine Optimization) details for this site. One of the things that the SEO peeps keep telling me is that I should have the title of the post in my URL (Universal Resource Locator). Instead of brianmahoney.ca/p123 , it seems that brianmahoney.ca/.htaccess-files-how-to-create-one .etc. would be better for search engines. I’d get more hits and life would be good. Here’s how you can set this up.

1.Inside of WordPress there is a setting for Permalinks. You can use the default (boring) setting or a custom setting that puts the name of the post in the URL. If you head to Settings then Permalinks, you’ll see a set of choices such as ‘Day and name’, ‘Month and name’, and so on. As you can see at the top of this page, I chose ‘Month and name’.

2. Choose ‘Month and name’ then choose ‘Custom Structure’. Once you choose ‘Month and name’, WordPress will generate the code which shows up in ‘Custom Structure’. Then click the dot to the left of ‘Custom Structure’ to choose it (first tell WP what structure you want, then it creates the desired code, then tell WP that you want to use it). WP then shows you the code  down near the bottom. My code looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

As it sits, this code is pretty meaningless unless you are an expert in Unix. This doesn’t matter because this code just has to be put into the .htaccess file on your server. What is an .htaccess file you say? I had the same question so I did some investigating. One of my sites had this file, the others didn’t. This file is used to control access to the files and folders on your site. If you don’t have a .htaccess file, it’s not too hard to make one. It’s just a text file with code in it, something like an .ini file in Windows, that controls who has the right to do what with your files. WordPress needs to be able to put the name of your post in the URL so it needs permission to do that.

3. Click Save Changes then see what WP says. On mine, it told me that ‘if my htaccess file was writeable, which it isn’t, blah blah blah.’ Turns out that I didn’t have an .htaccess file anyway. WordPress didn’t create one, even though it needed it, so I had to find one in another site’s root directory and copy it into the root directory of this site.

4. Either use FTP or your hosts File Manager and look at the root directory of your site. If there is an .htaccess file there, great. If not, don’t worry. You can create one easily. If you don’t have one that you can copy and upload into your site’s root directory, open Notepad and paste this into it:

Options +FollowSymLinks
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

This is my current .htaccess file and it should work with your server, if it’s an Apache server. It will probably work with any server as well. Your mileage may vary and don’t hold me responsible if this doesn’t work.

5. Save the file you have just created as whatever.txt or something like that. Once it’s up on your server you can rename it.

6. Using FTP or your host’s File Manager, upload this text file to the root directory of your site. That’s the directory that opens when you click on your site’s main folder.

7. Once the file is uploaded, rename it to .htaccess, making sure you put the period at the start.

8. Next, check the permissions of this file. Make sure that they are set to ‘644’ or what you see in this picture:

 

644 permissions for .htaccess file
Make sure the permissions are set this way.

9. Once you’ve set the permissions, click Change to save them. Open your site and make sure that it runs as it should.

TIP: Renaming files and changing permissions can be done in your FTP program or in your server’s File Manager. Try right clicking or hovering over a button to see how to do these things. There is no ‘one rule’ for every situation. This part you have to figure out on your own.

10. Remember that the permissions in your new .htaccess file are the same as mine: month and title of post in the url. If you don’t want that setting, go back to your Permalinks and change it. If WordPress says that it can’t write to the .htaccess file, copy and paste the code it gives you into Notepad. Head over to your site and check the actual .htaccess file to see if it matches the code. On mine, the .htaccess was fine EVEN THOUGH WP said it couldn’t write to the file. If the code isn’t the same, paste the new code into the file. TIP: There are many possible settings in the ‘htaccess file that control everything the folder it’s in and all of the folders below it in the directory structure. Right now, since you just created the file, there should only be the one setting in there. Highlight the setting that’s there and then paste the new setting into the same spot.

11. There are many custom setting for your WP permalinks out there on the ‘net. Non-default ones are called ‘pretty permalinks’. Mine isn’t that pretty but it should help with my SEO.

There is a lot of info in this article. I hope it’s clear and concise but if it isn’t, make a comment and I’ll try to help you out. Good luck! I should say that this seems a bit overwhelming at the start but it’s not that bad. The more you get to know what’s happening inside your server and inside Worpress, the more comfortable you will feel. Get your feet wet, dive in and learn.

Thanks for reading. Follow me on Twitter: @_BrianMahoney