Articles in the ModRewrite Category
Posted: Jun 05 | Filed under: ModRewrite
Well, sometimes this becomes a REAL problem. As soon as your site becomes popular people will start to hotlink your stuff, copy it, post it wherever they can.
Most likely you will even find your stuff on sites like mininova, piratebay and other torrent sites.
Worst of all, they even direct link to your downloads and images and make your bandwitdh go up dramatically.
So this time, we want to take a closer look on the htaccess file and fight!
Actually, many solutions out there are very very limited. Most people will only explain you how to prevent access to a file/folder completely or their solution does not work at all.
I was looking for a solution that would only allow direct access if the visitor would come from my own site and show up another image if they embed one of mine on their site.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mymainurl.net [NC] RewriteRule \.(jpg|jpeg|png|gif)$ http://mymainurl.net/angryman.gif [NC,R,L] SetEnvIfNoCase Referer "^http://(www.)?url1.net" spam_ref=1 SetEnvIfNoCase Referer "^http://(www.)?url2.com" spam_ref=2 SetEnvIfNoCase Referer "^http://(www.)?google.com" spam_ref=3 <filesmatch "(.rar)"> order deny,allow deny from all allow from env=spam_ref </filesmatch>
This is it, the best working solution to prevent hotlinking. It will only give access to vistors from google or your own site, plus show an image of your choice when someone tried to put one of your images on their website.
You will have to modify it a bit. Simply add the file extensions to
Also change the referrers that are allowed to hotlink “SetEnvIfNoCase Referer”.
Make sure to create an image called “angryman.gif” that will show up on other sites. It should contain your URL and a funny line to make the user enter the URL and leave the current website.
Posted: Dec 02 | Filed under: ModRewrite
Do you want to restrict access to certain folders only ?
Usually you will put a .htaccess into your main dir if your site should not be accesible to the public. That’s how you can easily avoid that it will get indexed by search engine spiders for example.
(more…)
Posted: Oct 20 | Filed under: ModRewrite
Well today I had to rewrite some URL’s from upper to lower-case letters. Somehow it did not rewrite this automatically. I didn’t figure out the reason why it did not work properly but I found a workaround.
If you have access to the httpd.conf file of the apache server, then simply add the following lines:
<VirtualHost IP:80> stuff here <IfModule mod_rewrite.c> RewriteEngine on rewritemap lowercase int:tolower RewriteCond $1 [A-Z] RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L] </IfModule>
Now it will redirect the URL’s properly. Yay!
Source
Posted: May 12 | Filed under: ModRewrite
If you want to redirect your root site to a subfolder then the following code can help you:
RedirectMatch ^/$ http://name.com/forums/
Add that to your .htaccess file after “RewriteEngine On”.
The ^ is a regular expression and stands for the the start string, the $ stands for the end string.
If you are new to RegEx I recommend the Regular Expression Cheat Sheet!
Posted: May 12 | Filed under: ModRewrite
If you want to hide your directory index so that people can’t see your files if they open a directory without a index file you have to add this to your .htaccess
Options -Indexes
Posted: Mar 21 | Filed under: ModRewrite
Due to SEO aspects many webmasters rewrite all their “www” queries to “non-www” queries. Why that ? Simply because search engines might think there is duplicate content if they can access www.yourdomain.com and http://yourdomain.com at the same time.
That’s why you should rewrite www to non-www or vice versa permanently.
Here is the simple solution for that:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^yourdomain\.com
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
Personally I think the shorter the better, but it’s different for each target group. Older people sometimes think that you have to type www in front of any domain name. This is completely up to you! Enjoy rewriting


