+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 22

Thread: .htaccess Help and Tips

  1. #1
    Join Date
    Nov 2005
    Location
    Planet Zeekois
    Posts
    380

    Thumbs up .htaccess mod_rewrite FAQ

    mod_rewrite

    Introduction.

    Welcome to mod_rewrite, the Swiss Army Knife of URL manipulation! Despite the tons of examples and docs, mod_rewrite is voodoo!

    This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.

    This module operates on the full URLs (including the path-info part) both in per-server context (httpd.conf) and per-directory context (.htaccess) and can even generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.

    This module was invented and originally written in April 1996. [1]

    How do I find out if the server supports mod_rewrite?

    At domain.com/.htaccess have
    Code:
    XBitHack Full
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index.page$ index.html [L]
    (If the index file isn't index .html, change that to what ever it is.)

    Then go to domain.com/index.page
    If the index page shows up then you got mod_rewrite. if you're on a Windows server, it probably won't work.

    How to change your URLs from dynamic to search engine friendly static URLs using mod_rewrite.

    Get an example of the dynamic URL and the way you want it. For example

    http://www.domain.com/cgi-bin/store.cgi?section=Nintendo&id=4867635&item=Pokemon
    and
    http://www.domain.com/store/Nintendo/4867635/Pokemon.html

    Now that you got both URLs, make a .htaccess file starting with...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^
    Depending on the server, you might not need the first two lines.

    Right after RewriteRule ^ enter the static URL, then a $, a space, and then original URL (with out the http://www.domain.com part for both URLs).

    You now got...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^store/Nintendo/4867635/Pokemon.html$ cgi-bin/store.cgi?section=Nintendo&id=4867635&item=Pokemon
    In the first URL, the static URL code, where ever the URL will change, replace it with a [^.]+) (Nintendo, 4867635
    and Pokemon in the example above).

    Then after .html add a $ and add a \ before the .html
    If you have a hyphen (-) or period in the new static URL, add a \ before it, for example...
    Code:
    RewriteRule ^store/[^.]+)/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=Nintendo&id=4867635&item=Pokemon
    If you don't add the \, you might get an Internal Server Error message, depending on the servers Apache version.

    Now in the static part of the URL where the URL changes, in the first change, change it to $1, then $2 and so on. Then add an [L] at the very end, with a space before the [L].

    You now got...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^store/[^.]+)/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2&item=$3 [L]
    Save the .htaccess file and upload it at domain.com/.htaccess and your static URLs will now work.
    http://www.domain.com/store/Nintendo/4867635/Pokemon.html
    instead of
    http://www.domain.com/cgi-bin/store.cgi?section=Nintendo&id=4867635&item=Pokemon


    Here's some other examples...
    Code:
    http://www.domain.com/cgi-bin/store.cgi?section=Nintendo&id=4867635
    RewriteRule ^store/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2 [L]
    
    http://www.domain.com/cgi-bin/store.cgi?section=Nintendo
    RewriteRule ^store/[^.]+)\.html$ cgi-bin/store.cgi?section=$1 [L]
    
    http://www.domain.com/cgi-bin/store.cgi
    RewriteRule ^index\.html$ cgi-bin/store.cgi [L]
    In this last example domain.com will show the index of the script. If the page shows nothing, try

    RewriteRule ^$ cgi-bin/store.cgi [L]

    With all the examples combined, you got...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^store/[^.]+)/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2&item=$3 [L]
    RewriteRule ^store/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2 [L]
    RewriteRule ^store/[^.]+)\.html$ cgi-bin/store.cgi?section=$1 [L]
    RewriteRule ^index\.html$ cgi-bin/store.cgi [L]
    Notice the order. If you list it as...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ cgi-bin/store.cgi [L]
    RewriteRule ^store/[^.]+)\.html$ cgi-bin/store.cgi?section=$1 [L]
    RewriteRule ^store/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2 [L]
    RewriteRule ^store/[^.]+)/[^.]+)/[^.]+)\.html$ cgi-bin/store.cgi?section=$1&id=$2&item=$3 [L]
    then mod_rewrite will freak out and it won't work! List the line with the most variables first, then the second most and so on.

    If you have more than one script, make sure you give each of them unique directory names or a different extension. For example, you can't let two different scripts change to product/WHATEVER.html but must give them for example product/whatever.html and product/whatever.htm (extension), or item/whatever.html and product/whatever.html (Directory name change.)

    Now upload the .htaccess so it's at domain.com/.htaccess

    Ack!!! Now it's messing up the rest of my site.

    If you have domain.com/index.html for example and have

    RewriteRule ^[^.]+)\.html$ store.cgi?section=$1 [L]

    in the code, make sure your mod_rewrited URLs use another extension, like .htm or .shtml or a unique directory name, for example...

    RewriteRule ^store/[^.]+)\.shtml$ store.cgi?section=$1 [L]

    I make the .htaccess changes, but the links still point to the same URLs.

    You must edit the script to make the links point to the new URLs. mod_rewrite only lets the fake URLs work.

    Can I have the .htaccess in a directory?

    Yes.

    In the above example, for having it at domain.com/store/.htaccess, change the code to...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /store/
    RewriteRule ^[^.]+)/[^.]+)/[^.]+)\.html$ /cgi-bin/store.cgi?section=$1&id=$2&item=3 [L]
    RewriteRule ^[^.]+)/[^.]+)\.html$ /cgi-bin/store.cgi?section=$1&id=$2 [L]
    RewriteRule ^[^.]+)\.html$ /cgi-bin/store.cgi?section=$1 [L]
    RewriteRule ^index\.html$ /cgi-bin/store.cgi [L]
    You moved store/ up to the RewriteBase line and added / before cgi-bin. If the script was in /store/store.cgi
    you would of had store/ instead of cgi-bin/ and then just got rid of it, to look like...
    Code:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /store/
    RewriteRule ^[^.]+)/[^.]+)/[^.]+)\.html$ store.cgi?section=$1&id=$2&item=3 [L]
    RewriteRule ^[^.]+)/[^.]+)\.html$ store.cgi?section=$1&id=$2 [L]
    RewriteRule ^[^.]+)\.html$ store.cgi?section=$1 [L]
    RewriteRule ^index\.html$ store.cgi [L]
    The URL to the index of the store will be domain.com/store/

    Don't search engines already index dynamic URLs?

    The biggest ones do like Yahoo, MSN, and Google, or it looks like they do. Here's a quote from some one who changed to mod_rewrite in November 2005...
    My site 771 pages was indexed by google and after implementing mod_write today google indexed over 9000 pages i would like to thanks Nintendo for starting such a thread that help me get better position. After doing all the optimization and with last google update i was ranking 80 for the most important keyword but today i am at 9th place visible on first page. Trafic is increasing from 1500 daily visitors to arround 4000 visitors.
    The original script URLs don't have the product name in the URL. Can I add the product name to the URL?

    Yes! If you can change the script to put the product names in the URL, or edit the links to link to them, yes you can. Here's an example. Notice there are two [^.]+)'s and no $2.

    RewriteRule ^[^.]+)/[^.]+)\.html$ cgi-bin/file.cgi?Item=$1 [L]

    Just edit the script links, or links in the static page to link to domain.com/whatever/PRODUCT_NAME.html have the product name show up where the last [^.]+) is in the .htaccess code.

    Can I rewrite a sub-domain to a directory?

    xxxxx.domain.com

    to

    www.domain.com/XXXXXX/
    Code:
    RewriteCond %{HTTP_HOST}   ^[www\.]*xxxxx.domain-name.com [NC]
    RewriteCond %{REQUEST_URL} !^/XXXXX/.*
    RewriteRule   ^[^.]+)  /XXXXX/$1 [L]
    According to mnemtsas on another message board.

    Does .htaccess increase server load?

    I have yet to ever see it increase server load on my dedicated server. IMO, that's just a rumor. I got about 30 domains with about 54 lines in the domain.com/.htaccess file and have yet to ever see it effect the server. The only effect I've ever got is getting GoogleBombed (Google chomping away at the static URLs so much that the server almost crashes or does crash!!!). Don't panic. This is why you have static URLs, to help search engines crawl your site.

    If you ever see high server loads or a slow server, try optimizing Apache.
    Last edited by Nintendo; 01-14-2006 at 04:35 AM.
    .

  2. #2
    Join Date
    Nov 2005
    Location
    Dordrecht NL
    Posts
    5,507
    Ah, we're privileged to have htaccess gurus like both you guyz here. Kudos
    Hey, DIY your SEO and smile!... Gotta love my host (no kidding)

  3. #3
    Join Date
    Nov 2005
    Location
    online
    Posts
    5,608

    .htaccess Help and Tips

    The .htaccess seems to be a popular question as well as an annoying problem at times. I'm compiling some of the best posts from across the forum to hopefully answer most of the frequently asked questions on this topic.

  4. #4
    Join Date
    Nov 2005
    Location
    online
    Posts
    5,608
    From hirenseo:
    Quote Originally Posted by hirenseo
    Hello,

    I am trying here to address all parameters.. but i am not sure how much accurate this is..please correct me if ...

    RewriteEngine On : It's apache rewrite engine.by default it's value is OFF. so need to change the value to ON.
    RewriteCond : It's rule condition and this line will execute on perticular condition.
    ^ is Start of line anchor.
    $ is End of line anchor.
    %{HTTP_HOST} it means anything before {HTTP_HOST} is allow.

    Now,

    Server-Variables: These are variables of the form

    %{ NAME_OF_VARIABLE }

    where NAME_OF_VARIABLE can be a string taken from the following list:
    HTTP headers: connection & request:

    HTTP_USER_AGENT - > REMOTE_ADDR
    HTTP_REFERER - > REMOTE_HOST
    HTTP_HOST - > REQUEST_METHOD



    You plople will get detailed information for this document :
    http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

  5. #5
    Join Date
    Nov 2005
    Location
    online
    Posts
    5,608
    From Mvandemar:
    Quote Originally Posted by mvandemar
    If the document and directory structure stays the same (ie. you move the whole site and the only thing that changes is the domain) you can do a wildcard 301. If everything changes, I would 301 all of the old pages to the homepage. If there are changes, but by directory (/forums/ moves over, but the rest doesn't) then you can do a wildcard by directory. But if you manually change the names of all the pages you're stuck, unless you want to a) keep the old domain for 301 purposes and b) keep a database of the old to new mapping. The last option isn't as hard as it sounds, and it does remind me to suggest to you that you still retain the old domain name for as long as you can (ie. keep renewing it). If you lose it you lose the 301's.

    -Michael
    and followed up with this post...
    Quote Originally Posted by mvandemar
    That's easy then.

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^bransoncritic.org 
    RewriteRule (.*) http://www.bransoncritic.com/$1 [R=301,L] 
    
    RewriteCond %{HTTP_HOST} ^www.bransoncritic.org
    RewriteRule (.*) http://www.bransoncritic.com/$1 [R=301,L] 
    
    RewriteCond %{HTTP_HOST} ^bransoncritic.com 
    RewriteRule (.*) http://www.bransoncritic.com/$1 [R=301,L]
    The last one does the non-www to the www for the .com, might already be there, I included it just in case.

    -Michael
    Last edited by skitzzo; 07-09-2006 at 01:54 PM.

  6. #6
    Join Date
    Nov 2005
    Posts
    4,528
    Also, something in that same project that I should have remembered but overlooked, that goes to anyone who is planning to redirect non-www versions of a site to www versions. Certain website software licenses or installation configurations are domain specific. I believe that SMF is one such. This means that in certain situations, if you have 3rd party software installed on your site under, say, the non-www version, it may not work if you do a sitewide change such as is listed above. Two things to keep in mind with this:
    a) Not everything is like this, and it does not mean that it will definitely break, just that you should always test everything after a change like this and be ready to roll back in needed, and
    b) The SEO benefit works regardless of which way you make the redirect go. In other words it doesn't really matter if you have everything redirect to the www version or if you have everything redirect to the non-www one, as long as it is consistent.

    -Michael
    Last edited by mvandemar; 07-10-2006 at 06:04 AM. Reason: fixing italics
    Free Image Meta Search
    SEO Tools
    Poetry

    Blog Smackdown!
    Skitz is very clear about what behaviour caused him to start this site up. His ego is way too big and fully engaged - rmccarley

    It was just too much text. I couldn't read it. - rmccarley

    I'm not a prosecutor, but Dave thinks I should play one on T.V.

  7. #7
    Join Date
    Nov 2005
    Location
    Planet Zeekois
    Posts
    380

  8. #8
    Join Date
    Nov 2005
    Location
    God's Own country
    Posts
    2,612
    Just found one good resource with some of the .htaccess tricks inside...
    felt good and you may also...

    http://www.javascriptkit.com/howto/htaccess.shtml
    Search Engine Marketing India "With a Virtue of being Practical...!!!".
    DD:"If you can't explain it simply, you don't understand it well enough."

  9. #9
    Join Date
    Nov 2005
    Location
    Sacramento, CA
    Posts
    6,213
    Didn't know tis was here! (Bad Mod - no donut!).

    Anyway, there's always the canonical fix which is a must for every website to make sure you get the most value from your backlinks and avoid duplicate content issues.
    |
    Clue in soon!

  10. #10
    Join Date
    Nov 2005
    Location
    I've heard it's important
    Posts
    1,463
    This is a very old thread, but I have something I need help with, and MV almost gets me to my answer in this thread - I just need a wee bit more help.

    I'm trying to 301 redirect (via htaccess) oldsite.com/forum to newsite.com/forum - oldsite.com has other content in it that will not be redirected, it's just the forum we're moving, giving it a new home all to itself. The desired end result is to redirect any forum thread from the old site to the same forum thread at the new site. It looks like a simple wildcard is needed, but I'll be damned if I can find the info I need to make that happen (5 pages of SEOR results and 10 Googles later...).

    Can someone throw this dog a bone?

    Thx

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Bookmarks

Posting Permissions

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