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 »
8 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" )
}

Maintenance mode for apache

Doing some work on your web site but you don't want other people to see what you're doing. Give them a redirect (like the flickr massage screen).
You need to use your own ip of course.

# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403 http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from 99.88.77.66

301 Permanent Redirect rule to consolidate domains

// redirect domain.com to www.domain.com (or vice versa)
// helps substantially with delicious links and other SEO optimization
// see for discussion

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

'Site is under maintenance' redirect, except for you

Upgrades woohoo! Tell those lusers to hold their horses while you happily debug. Some .htaccess magic, just sub in your IP:

<IfModule mod_rewrite.c>
        RewriteEngine On
        # 'under construction' override
        RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
        RewriteRule ^(.*)$ maintenance.html
IfModule>

PHP - Using Absolute URLs with header()

// Redirects a user to the current domain, the current directory, and the new page. Won't break if you move the scripts to another folder/domain etc.

header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/newpage.php");

Easy 301 or 302 redirect using .htaccess

Here is an easy way to redirect one url to another if you don't need any fancy regex pattern matching.

Permanent 301 redirect
Redirect permanent /relative_source_url http://full_destination_url/


-or-

Temporary 302 redirect
Redirect temporary /relative_source_url http://full_destination_url/

www to no-www, and vice-versa

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

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


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

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

Redirect domain.com requests to www.domain.com

Make sure you have mod_redirect in server.modules, then add this to your lighttpd.conf:

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


I wasn't able to come up with a way to do it in a single rule. It seems that only "" (not even "(.*)") will match the root request.
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed