Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed 

Lighttpd redirect www to no www

Catch all requests for www.domain.tld and redirect to domain.tld

$HTTP["host"] =~ "^www\.(.*)" {
   url.redirect = ( "^/(.*)" => "http://%1/$1" )

Scrape torrents on btjunkie

// download all .torrent links on the btjunkie frontpage
// more fun with mechanize @

agent =
links ='.tor_details tr a')
hrefs = { |m| m['href'] }.select { |u| u =~ /\.torrent$/ } # just links ending in .torrent
FileUtils.mkdir_p('btjunkie-torrents') # keep it neat
hrefs.each { |torrent|
  filename = "btjunkie-torrents/#{torrent[0].split('/')[-2]}"
  puts "Saving #{torrent} as #{filename}"

Scrape MySpace friend thumbnails

// fetch all img's from a myspace profile's .friendSpace div
// more @

agent =
links ='.friendSpace img') # found w/ firebug
FileUtils.mkdir_p 'myspace-images' # make the images dir
links.each_with_index { |link, index| 
  url = link['src']
  puts "Saving thumbnail #{url}"
  agent.get(url).save_as("myspace-images/top_friend#{index}_#{File.basename url}")

Scrape YouTube thumbnails

// fun with mechanize
// more @

agent =
url = "" # all time
page = agent.get(url)
# parse again w/ Hpcricot for some XML convenience
doc = Hpricot.parse(page.body)
# pp (doc/:entry) # like "search"; cool division overload
images = (doc/'media:thumbnail') # use strings instead of symbols for namespaces
FileUtils.mkdir_p 'youtube-images' # make the images dir
urls = { |i| i[:url] }
urls.each_with_index do |file,index|
  puts "Saving image #{file}"
  agent.get(file).save_as("youtube-images/vid#{index}_#{File.basename file}")

Common Lighttpd rewrite requests

More than anything, this is for my own reference, but also for the benefit of jordanbrock...

This comes courtesy

For various reason you might want to redirect all traffic coming to to or vise versa.

This is essential if you want to maintained good structure for search engines (SEO) or to generate accurate stats.

Open your /etc/lighttpd/lighttpd.conf file:
# vi /etc/lighttpd/lighttpd.conf

Find your virtual domain section.

h4. Add following configuration directive if you want to redirect to

$HTTP["host"] =~ "^www\.(.*)" { url.redirect = ( "^/(.*)" => "http://%1/$1" ) }

h4. Add following configuration directive if you want to redirect to

$HTTP["host"] =~ "^domain\.com$" { url.redirect = ( "^/(.*)" => "$1" ) }

Close and save the file. Restart the lighttpd web server:
# /etc/init.d/lighttpd restart

www to no-www, and vice-versa

To redirect requests for to (without the www) put this in your .htaccess:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

And to do the reverse (redirect non-www to www), try this:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed