Need help rewriting an Nginx rule for subdomain
I did a little rewrite rule a little while ago to redirect ppl who directly access my site's image to image pages instead,
for example a person accessing www.mysite.com/i/asdf.jpg to www.mysite.com/pic/asdf
this is the rewrite rule i used :
location /i/image_(\d+).(gif|jpg|jpeg|png)$ {
root /home/mysite/public_html;
valid_referers www.example.com mysite.com;
if ($invalid_referer) {
rewrite ^ http://www.example.com/pic/$1 permanent;
}
}
I made a subdomain of the directory 'i' which contains all the images. so its like thsi now http://i.mysite.com/
Is it possible to make a rewrite like the one above so if the file is directly accessed by a different referer it will hit the same rewrite rule ?
thx :)
UPDATE : now using this :
server {
access_log off;
error_log logs/vhost-error_log warn;
listen 80;
server_name i.imgzzz.com;
root /home/imgzzz/public_html;
#location ~ ^/pic/ { alias /home/imgzzz/public_html/i; }
location ~ ^/pic/(.*)$ { alias /home/imgzzz/public_html/i/$1; }
valid_referers i.imgzzz.com www.imgzzz.com imgzzz.com;
location ~ /image_(\d+).(gif|jpg|jpeg|png)$ {
if ($invalid_referer) {
rewrite /image_(\d+).(gif|jpg|jpeg|png)$ /pic/image_$1 last;
}
}
}
Htaccess rewrites :
RewriteBase /
Options +FollowSymlinks
#RewriteEngine on
rewritecond %{http_host} ^imgzzz.com [nc]
rewriterule ^(.*)$ http://www.imgzzz.com/$1 [r=301,nc]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|jpeg|gif) pic/$1 [L]
RewriteRule ^pic/([0-9]+)/{0,1}$ view.php?name=$1 [QSA,L]
RewriteRule ^pic/full/([0-9]+)/{0,1}$ view-f.php?name=$1 [QSA,L]
RewriteRule ^pic/([0-9]+)/feature/$ view.php?name=$1&a开发者_Go百科mp;pop=1 [QSA,L]
valid_referers i.imgzzz.com www.imgzzz.com imgzzz.com; location ~ /image_(\d+).(gif|jpg|jpeg|png)$ { if ($invalid_referer) { rewrite /image_(\d+).(gif|jpg|jpeg|png)$ /view.php?name=image_$1.$2 last; } }
精彩评论