Htaccess, Apache, and Rewrites! Oh, My!
Total Page:16
File Type:pdf, Size:1020Kb
Crucial Web Hosting » Blog » Htaccess, Apache, A... http://www.crucialwebhost.com/blog/htaccess-apa... Home Blog Htaccess, Apache, And Rewrites! Oh, My! Htaccess, Apache, And Rewrites! Oh, My! As a web designer or developer, it is important to know how to use the htaccess file to your advantage. It is a very powerful tool, and can even work as a deterrent for bandwidth thieves, exploits, and hackers. Below are some common examples of rules to consider when developing websites. We hope you find them useful. Jump To Redirect Example 1. Rewrite Engine 2. Basic Redirect 3. HTML to PHP 4. Remove File Extensions 5. Add WWW To Domain 6. Remove WWW From Domain 7. Add Trailing Slash 8. Remove Trailing Slash 9. Remove Query String 10. Remove Trailing Question Mark 11. Remove Trailing Index File 12. Remove Index File From URL 13. HTTP to HTTPS 14. HTTPS to HTTP 15. Deny Access To Htaccess 16. Enable Caching 17. Disable Caching On Dynamic Pages 18. Example Htaccess File Rewrite Engine In order for any of these rules to work, the first thing we need to do is turn on the rewrite engine. 1. Options +FollowSymLinks 2. RewriteEngine On 1 of 17 04/11/2008 02:00 PM Crucial Web Hosting » Blog » Htaccess, Apache, A... http://www.crucialwebhost.com/blog/htaccess-apa... The first line of code is a security feature that must be enabled for the rewrite rules to work. In most cases, this is already set up by your host, but it won’t hurt to list it again here. This is just telling the server to follow symbolic links. If you are a Windows user, this would be the same as a shortcut. You may not need the first line though, but it is important to understand why you might need to add it. The second line is what actually turns the rewrite engine on. It does nothing for us though. Basic Redirect There are a couple reasons why you would use a basic redirect. Let’s say you just re-structured and organized your files, but you wanted visitors who were still using the old filename to be able to access the new one. They might have bookmarked the page, or found it in a search engine. 1. RewriteRule ^old-filename.php$ /new-filename.php [R=301,L] The first part of the rule is looking for a request for old-filename.php. If it finds it, it will visually redirect the visitor to new-filename.php. In the last part of the rule, we’re using a redirect flag with a 301 code attribute attached, which is a permanent redirect. If we had just used [R] without adding the code parameter, the code would have been a 302, which is a temporary redirect. We want to use a permanent one so search engines know that the old file does not exist anymore. There is also a caret in front of old-filename.php and a dollar sign at the end. These are regular expressions, and the caret tells us to the request must start with this, while the dollar sign means it must end with this. We will talk more about flags and regular expressions in an upcoming article. The last thing we want to point out is the forward slash in front of new-filename.php. If we were to specify the base path for rewriting, we would not need this, but since we did not, we need to add it. If we wanted to drop it though, we would add the following directive after we turn the rewrite engine on: 1. RewriteBase / It just depends on what you find easier. The base for rewriting URLs will always be the root of your website. There is also another way to perform redirects without using mod_rewrite. We can use mod_alias instead: 2 of 17 04/11/2008 02:00 PM Crucial Web Hosting » Blog » Htaccess, Apache, A... http://www.crucialwebhost.com/blog/htaccess-apa... 1. Redirect 301 /old-filename.php http://www.domain.com/new-filename.php 2. Redirect Permanent /old-filename.php http://www.domain.com/new-filename.php Both lines of code work, but you only need to use one. We could also do a temporary redirect: 1. Redirect /old-filename.php http://www.domain.com/new-filename.php 2. Redirect 302 /old-filename.php http://www.domain.com/new-filename.php 3. Redirect Temp /old-filename.php http://www.domain.com/new-filename.php All three lines would work, but we only need to use one. If you don’t specify anything after the Redirect, it will use the default temporary redirect (a 302), just like our redirect flag. HTML to PHP What if we recently converted our site to PHP, but all of the old filenames were using the .html extension? It wouldn’t make sense to create a redirect rule for each file. So instead we could do this: 1. RewriteRule ^(.*).html /$1.php [R=301,L] Anytime a request for a file with the .html extension is made, it will be redirected to the same file but with our new .php extension. Of course, we could always change the way Apache handled HTML files, letting them act like PHP files instead. 1. AddType application/x-httpd-php .php .html .htm Remove File Extensions But file extensions are so ugly! Maybe you want to give the illusion that your individual files are actually directories: 1. RewriteRule ^(.*)/$ /$1.php [L] So we could have a bunch of files in our root directory that looked like this: 1. http://www.domain.com/services.php 2. http://www.domain.com/products.php 3. http://www.domain.com/about.php 3 of 17 04/11/2008 02:00 PM Crucial Web Hosting » Blog » Htaccess, Apache, A... http://www.crucialwebhost.com/blog/htaccess-apa... 4. http://www.domain.com/links.php 5. http://www.domain.com/contact.php And they could instead look like this: 1. http://www.domain.com/services/ 2. http://www.domain.com/products/ 3. http://www.domain.com/about/ 4. http://www.domain.com/links/ 5. http://www.domain.com/contact/ Add WWW To Domain One of the first things you should decide when you create a new site, or at least early on, is if you’re going to have the WWW in your domain or not. This is important not only aesthetically, but for search engine optimization. By forcing visitors and search engines to your preferred domain, you can guarantee that you won’t end up with duplicate results or different page ranks for your domain with or without the WWW. I personally think domains look naked without them, and there is a reason why we use the WWW in the first place. See, not everyone enters the WWW when they type in a domain. Some leave it out, while others always type it in. If you’re familiar with the keyboard shortcuts built in to your browser, simply typing the domain without the TLD extension (e.g. domain instead of domain.com) and then holding CTRL + Enter will add the www. before the domain name, and a .com after it (other keyboard shortcuts will use .org or .net). You might have been to a site before and noticed that if you don’t type the WWW in, you get an error page telling you that the page cannot be found. It all depends on how your host or server administrator has decided to set this up, but usually your domain will work with or without the WWW. 1. RewriteCond %{HTTP_HOST} ^domain.com$ [NC] 2. RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] The first line is a rewrite condition that is looking at the current hostname, which can be something like www.domain.com, domain.com, sub.domain.com, or an IP address. The second part of this line is checking to see if the hostname does not have a WWW in front of it, and if it does not find one, it moves to the second line, and redirects all requests 4 of 17 04/11/2008 02:00 PM Crucial Web Hosting » Blog » Htaccess, Apache, A... http://www.crucialwebhost.com/blog/htaccess-apa... to the same domain with the WWW in front of it. If the WWW is already there, then this rule is ignored because it would not meet the condition in the first line. The NC flag at the end of the first line just means to ignore the case of what the request is, so we could type in DoMain.com and it wouldn’t matter. The R=301 flag means we are going to visually redirect the visitor to our domain with the WWW. A 301 code is a permanent redirect, and that is important because it also tells search engines that this isn’t a temporary thing (which is what a 302 code would be). The L flag means this is the last rule to follow in this set. Any other rules or conditions after this will not be factored into this rule. Remove WWW From Domain Others prefer to remove the WWW, and for this rule, we’re just doing the exact opposite of the previous example. 1. RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] 2. RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] Add Trailing Slash The use of a trailing slash is also important to consider. By default, your web browser will add a trailing slash to the end of a URL. It makes sense to have the trailing slash too—except for files—because it means we’re in a directory.