Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post
    My client asked if I could add a rewrite rule to their web site so that when a user tries to display site.com/anything it will be redirected to www.site.com/anything. (The reason is to improve search engine rankings by not splitting search engine queries across two "server names," www and null.)

    I figured out how to do this by putting a .htaccess in the root directory with this contents:

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^site.com [nc]
    rewriterule ^(.*)$ http://www.site.com/$1 [r=301,nc]
    I've tried this on our QA site. It works.

    I've got one problem with it: unlike every other file on the site, it incorporates the domain name. Consequently I need different versions of the file for the QA site and the live site -- and I must remember forever not to upload the QA version to production, or the production version to QA.

    I wonder if there's a way to rewrite (pun intended) the rewritecond and rewriterule to eliminate explicit references to the domain name, so that I won't have this problem.

    Note, this is not a question about regular expression syntax. I could easily solve the problem if I were performing a substitution in a PHP script, for example. Instead, this is a question about rewritecond and rewriterule. After studying their descriptions in apache.org's documentation, it appears to me that the .htaccess file above should not work. But that's how the example I found was written; I tried it and it worked; so I don't understand what it's doing, and I can't think effectively about how any sort of change would affect it.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Any time I edit an .htaccess file I know I'm in for 30-90 minutes of frustration...

    Are you saying that you have two different host names? You can include rules for two different hosts in the same file (the rule applied will be the one which matches rewritecond). In the brackets after the [R=301] you might want to include L to indicate that no further rules should be processed.

    Edit: By the way, my files have \ before the periods in the domain pattern:

    RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post
    Are you saying that you have two different host names?
    Yes, one for the live site and one for the test site.

    I tried your suggestion, and it works. Even makes sense, as much as any of .htaccess makes sense. I'm leaving out the L, though. The file works without it, and I'd rather leave well enough alone.

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •