.htaccess Snippets — A collection of useful .htaccess
snippets.
Tag: .htaccess
Yesterday, I needed to move a self-hosted WordPress site to another domain. The process was easy because it’s like copying all files, edit the configuration file and editing internal links in all posts. All process only took less than 15 minutes. But, that’s not the only thing. One of the important things needed is to maintain the article links — known as “permanent link”.
This is important because I don’t want to send the visitors coming from other sources (links in blog posts, shared link on Twitter or Facebook) to missing pages. It’s called “permanent link”, right? So, having the permanent link broken is not a good idea.
I came up with a simple solution: using .htacess
. After moving all files and checking all configurations I put these lines in the .htaccess
file:
RewriteEngine On RewriteCond %{HTTP_HOST} ^.*olddomain.com$ [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
So, when visitors visit olddomain.com/path/to/article/
, the browsers will go automatically to newdomain.com/path/to/article/
. Easy!
These days, there are many social services we use. I use some of them. Bloggers or websites also provide some links on their blogs/websites to those social service. If links to our profile is easy to remember, that should be great. For example, it’s easy to remember my Flickr photostream using http://flickr.com/orangescale, or Twitter account (http://twitter.com/thomasarie).
But, what about Facebook, Technorati, or LinkedIn? My Facebook profile page is located here: http://www.facebook.com/people/Thomas-Arie-Setiawan/695771665. There is a Facebook application to create more memorable Facebook profile, page or group. They will be something like profile.to/thomasarie or groups.to/AnyGroupName.
Few days ago, @mahadewa and @huda mentioned about this. Anyway, there is .htaccess for redirection. So, why not using it to produce more memorable URLs for our profile? It’s easy. Now, I can have https://thomasarie.com/facebook (Facebook profile), https://thomasarie.com/linkedin (LinkedIn profile), and more.
Since [added later: I have my own domain name,] my web hosting uses Apache and supports .htaccess
, it’s very easy to do it. I just put these lines in my .htaccess
(in the root folder).
Redirect 301 /facebook http://www.facebook.com/people/Thomas-Arie-Setiawan/695771665 Redirect 301 /flickr http://flickr.com/orangescale Redirect 301 /linkedin http://www.linkedin.com/in/thomasarie Redirect 301 /openid http://openid.orangescale.com/thomasarie Redirect 301 /technorati http://technorati.com/people/technorati/thomasarie
“Redirect 301” means permanent redirection. You can add as many redirection rules there. Just see the patterns there, pretty easy to understand, right? And, I think it’s easier to remember. It also nice if we use it for our business card :)
I am currently working on a WordPress-powered site. One of its features is a download section. After comparing some WordPress plugins, I decided to use Lester ‘GaMerZ’ Chan‘s WP-DownloadManager.
When I use that plugin on WordPress 2.5.1, I got an error. It’s something like this:
Warning: cannot yet handle MBCS in html_entity_decode()! in /home/.../wp-downloadmanager.php on line 308
Is it a bug? It seems that I got this error message because this plugin needs PHP 5 (I still use PHP 4). Since many hosts having PHP 5-enabled (even it does not always installed as the default version), it should not be that difficult to solve this issue. All I need to do is making all .php files are treated as PHP 5 files. Okey, this is a straight forward solution: add a new line in your .htaccess
file. You should find it in your WordPress directory, if not, just add this line:
AddType application/x-httpd-php5 .php
And, the problem solved.
Update: Put AddType application/x-httpd-php5 .php
in the first line in .htaccess
file. Here is mine:
AddHandler application/x-httpd-php5 .php # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
After that, create a new php file e.g. myphpinfo.php
and include this line:
<?php phpinfo(); ?>
This will display about PHP version (affected by .htaccess
).
Web statistics script for this site
I use some scripts to gain information about the site statisics. The first one is Shaun Inman’s Shortstat, and the second one is refer script by Textism. About the first, Thalia mentioned something about the database rows. Yes, you’re right Thalia. Well, this is how I use those scripts.
Easy-to-read statistics
Shaun Inman’s Shortstats gives me something easy to read. And, it is quite complete. I can have these information: total hits, unique visitors, browsers, referers, search strings, and visitors’ country origins. When I write this, there are thousands of rows in my statistics table.