301 Redirects with Apache .htacess file

I’ve just spent more time than I care to in the last couple of days trying to figure out rewriting urls with Apache, both through .htaccess files and directly in the conf file. Today was relatively painless, as I just had to come up with a way to 301 redirect all “extra domains” and some specific pages. For good measure, I also wanted to redirect non “www” domains to the canonical domain and also allow for future, yet unknown domains to be redirected. Here is what I came up with for an .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301]
redirect 301 /log.php http://www.yourdomain.com/fog.php

Pretty simple – the RewriteCond looks for any domain other than the one specified and upon matching, invokes the RewriteRule. The RewriteRuule simply uses a 301 redirect to the domain. The last line just redirects to a specific page. Looking at the output of Live HTTP Headers for firefox I noticed that it issues two 301 redirects if someone where to type http://www.extradomain.com/log.php. One redirect to get the proper domain, http://www.yourdomain.com/log.php, and then a second redirect for the actual page. Not a big deal, but I thought it would handle it all in one redirect since I wasn’t using the L flag. I know, I have a lot to learn when it comes to mod_rewrite…

On a side note, why is it soooo diffucult to get 301 redirects in place from other people. Domain registers seem to only use 302 redirects and whenever I request a 301 redirect for a domain from another company, I either get silly responses like, “You can use the PHP header function to do a 301 redirect” (these are sites with a lots of individual html and php files, not a app running with a front controller) or it ends up being a 302 redirect.


While I am complaining, I might as well mention the bad behavior I experienced while cooking up some redirects. On my Fedora Core 5 desktop, I edited my /etc/hosts to include the domains and then set up a virtual host with aliases for the domains on my Apache server – this allowed me to use my browser and test the redirects locally with Live HTTP Headers. It seemed that the redirects would occasionally get cached somehow? Not sure whether it was my browser or on the server, but I was changing the .htaccess file and the changes would not take effect. If I cleared my browser cache/cookies AND restarted Apache, things seemed to clear up. But I thought Apache read .htaccess file on every request, and thus, changes should instantly take effect? If it was Firefox, then why didn’t clearing the cache/personal data have any reliable effect? Maybe it was my desktop? Who knows…

2 Responses to “301 Redirects with Apache .htacess file”

  1. Cleva Williams Says:

    hi. i’m sorry this isn’t quite relevant to your article above, but a google search brought me here while looking for info on my issue– and you seem like the type of guy who does research and experimentation in a way familiar to my own, so… i beg your pardon for my inquiry…

    if i’m typing in simply http://localhost/ on FC5, expecting to get the “default Fedora / Apache success page, as i have in the few previous Fedora / Apache installations i’ve done w/ greater success on a few other HDD’s , but instead i’m getting a save-as dialogue as if Apache isn’t recognizing the filetype. i followed the diety’s notes step-by-step at Standton-Finley.net, but as you can see here (i think) http://tinyurl.com/mbahj — she ain’t workin’

    in your research de-la-Apache, what would you suggest might be a “Rule of Thumb”, or even– a quick reference for this type of thing?

    rock on, RB. i hope this comment doesn’t detract from the meaning of your artcile here– it certainly wasn’t my intent to do so. if it’s totally off-base, please feel free to erase.

  2. Administrator Says:

    Yeah, looks like Apache is not recongnizing the PHP file. Did you install PHP? In your httpd.conf file, do a search for “php”. Do you see a loadModule for php5 or php4? If not, do you see this line “Include conf.d/* .conf” and have a php.conf file in your conf.d directory?

    I don’t know if there is a rule of thumb. Open source provides a lot of flexiblity, but it also requires a decent amount of troubleshooting….

Leave a Reply