Never been to DZone Snippets 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

« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS 

Apache rewrite rules

Pass all requests for non-existing files or directories to index.php
RewriteCond        %{REQUEST_FILENAME}        !-f                
RewriteCond        %{REQUEST_FILENAME}        !-d
RewriteRule        ^(.*)$                    index.php        [L]

Using Apache with RewriteMap and a text file

This code will read a text file using Apache then upon a user request, look for a url containing /l/ eg. http://mysite.com/l/digg and rewrite the URL in full eg. http://mysite.com/gwd/feed/digg.html . The example file is shown here for completeness.

RewriteMap uses a text file as a convenient alternative for the administrator when declaring many rewrite rules. Code based on the article from ONLamp.com - A Day in the Life of #Apache http://urltea.com/1vwb


<file name="links.txt" location="/var/www/localhost/">
scotsman /gwd/feed/scotsman.html
digg /gwd/feed/digg.html
</file>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap links txt:/var/www/localhost/links.txt
RewriteRule ^/l/(.*) ${links:$1|http://mysite.com/} [R]
</IfModule>



Note: The [R] at the end of RewriteRule means redirect, to have a clean url simply remove that switch.

*update 15-Feb-08 *
Restart Apache rather reloading the module when switching redirection on or off for a RewriteRule. The Apache version 2.2.6 (Unix) didn't pick up my settings correctly when I tried /etc/init.d/apache2 reload, instead I needed to use /etc/init.d/apache2 restart.

A simple mod_rewrite example

Rewrite a long url as a short easy to remember url with mod_rewrite. Place this code in httpd.conf or the virtual hosts file. Original code from sitepoint - mod_rewrite: A Beginner's Guide to URL Rewriting http://urltea.com/1v6d .

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/shortcut$ /complicated/and/way/too/long/url/here
</IfModule>

slashdot protection

Replace aaa.bbb.ccc.ddd with your own IP (so you always get to your site).
Replace www.yourdomain.com with your actual domain.

Just don’t mess with the user_agent and query_string lines. Those ensure that the Coral servers themselves can retrieve your page when they need to.

(Shamelessly stolen from http://ottodestruct.com/diggprotectionrules.txt)

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^aaa.bbb.ccc.ddd$
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteCond %{QUERY_STRING} !(^|&)coral-no-serve$
RewriteCond %{HTTP_REFERER} ^http://(www\.)?digg\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?slashdot\.org [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?slashdot\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?fark\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?somethingawful\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?kuro5hin\.org [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?engadget\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?boingboing\.net [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?del\.icio\.us
RewriteRule ^(.*)$ http://www.yourdomain.com.nyud.net:8080$1 [R,L]

Trac installation on its own subdomain

Most installation documents of Trac show how to install to a directory in the main site's domain. For installation into a subdomain, the Trac data and CGI script should be installed into trac.domain.com/htdocs/trac/, with the following in the Apache config file:

<Directory /path/to/trac.domain.com/htdocs/trac/>
  AllowOverride None
  Options ExecCGI
  AddHandler cgi-script .cgi
  Order allow,deny
  Allow from all
</Directory>

<VirtualHost 1.2.3.4>
  ServerName trac.domain.com
  DocumentRoot /path/to/trac.domain.com/htdocs/

  RewriteEngine On
  RewriteCond %{REQUEST_URI} !^/cgi-bin/
  RewriteCond %{REQUEST_URI} !^/trac/
  RewriteRule ^/(.*) /trac/trac.cgi/$1 [L]

  <Location />
    SetEnv TRAC_ENV "/var/lib/trac/projname">
  </Location>
</VirtualHost>

Redirect port 80 to port 443 (ssl)

RewriteEngine On 
RewriteCond  %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.de/folder/$1 [L,R]

Making symbolic linked Rails apps work when folder accessed with no trailing slash

Snippets is at bigbold.com/snippets/ but bigbold.com/snippets always failed with "Bad Request". bigbold.com/snippets always seemed to rewrite into 'snippets.html' for some reason, so this change to .htaccess cures it:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !snippets\.html
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteRule snippets\.html /snippets/ [R]


Replace snippets with the name of your own folder, obviously.

Block Google Web Accelerator with .htaccess and mod_rewrite

By Shane Allen.

RewriteEngine On
RewriteCond %{HTTP_X_MOZ} ^prefetch$
RewriteRule ^.*$ - [F]

301 example.com to www.example.com

RewriteEngine On

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

Prevent yourself from getting Slashdotted

Assuming you have mod_rewrite and AllowOverride activated, throw this in the .htaccess file for the directory you don't want to be Slashdotted:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^.*slashdot.*$
RewriteRule .* - [F]


This all assumes the editors actually check the links before posting the story, but this should reasonably mean no-one on Slashdot can link to you, even if it's a bit over the top.
« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS