.htaccess و کاربردهای آن

موارد کاربرد فایل .htaccess
1. Set TimeZone
تنظیم زمان سایت با کد زیر:
1 | SetEnv TZ Asia/Tehran |
2. 301 permanent Redirect
استفاده از 301 Permanent Redirects برای استفاده در موتور های جستجو
1 | Redirect 301 http: //www.domain.com/home http://www.domain.com/ |
3. اضافه کردن mime-type
حذف پرسش سرور پیرامون اینکه فایل را باز می نمایید و یا دانلود می کنید. براحتی فایل مورد نظر را download نمایید.
1 | AddType application/octet-stream .pdf |
1 | AddType application/octet-stream .zip |
1 | AddType application/octet-stream .mov |
4. حذف www
ارجاع تمام درخواست ها برای باز شدن سایت با www به URL سایت که به بهینه سازی برای موتورهای جستجوگر کمک می نماید.
1 | RewriteEngine On |
1 | RewriteBase / |
1 | RewriteCond %{HTTP_HOST} ^www.domain.com [NC] |
1 | RewriteRule ^(.*)$ http: //domain.com/$1 [L,R=301] |
5. Custom Error page
ایجاد نمایش خطاهای دلخواه و مسیردهی آن
1 | ErrorDocument 401 /error/401.php |
1 | ErrorDocument 403 /error/403.php |
1 | ErrorDocument 404 /error/404.php |
1 | ErrorDocument 500 /error/500.php |
6. Compress files
بهینه سازی سرعت load سایت با فشرده سازی فایل ها
1 | # compress text, html, javascript, css, xml: |
1 | AddOutputFilterByType DEFLATE text/plain |
1 | AddOutputFilterByType DEFLATE text/html |
1 | AddOutputFilterByType DEFLATE text/xml |
1 | AddOutputFilterByType DEFLATE text/css |
1 | AddOutputFilterByType DEFLATE application/xml |
1 | AddOutputFilterByType DEFLATE application/xhtml+xml |
1 | AddOutputFilterByType DEFLATE application/rss+xml |
1 | AddOutputFilterByType DEFLATE application/javascript |
1 | AddOutputFilterByType DEFLATE application/x-javascript |
7. Cache files
Cache یکی دیگر از روش های مشهور بهینه سازی load سایت
1 | FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$ |
1 | Header set Cache-Control "max-age=2592000" |
1 | /FilesMatch |
8. Disable caching for certain file type
شما می توانید cache برای نوع فایل دلخواهی غیرفعال نمایید.
1 | # explicitly disable caching for scripts and other dynamic files |
1 | FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$ |
1 | Header unset Cache-Control |
1 | /FilesMatch |
امنیت
9. Hotlinking protection with .htaccess
برای جلوگیری از استفاده تصاویر سایت شما مصرف پهنای باند سایت شما از طریق کد زیر قادر به جلوگیری خواهید بود:
1 | RewriteBase / |
1 | RewriteCond %{HTTP_REFERER} !^$ |
1 | RewriteCond %{HTTP_REFERER} !^http: //(www.)?queness.com/.*$ [NC] |
1 | RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L] |
10. Prevent hacks
با استفاده از خطوط زیر می توانید از چند روش رایج هک شدن سایت خود جلوگیری نمایید.
1 | RewriteEngine On |
1 | # proc/self/environ? no way! |
1 | RewriteCond %{QUERY_STRING} proc/self/environ [OR] |
1 | # Block out any script trying to set a mosConfig value through the URL |
1 | RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR] |
1 | # Block out any script trying to base64_encode crap to send via URL |
1 | RewriteCond %{QUERY_STRING} base64_encode .*(.*) [OR] |
1 | # Block out any script that includes a script tag in URL |
1 | RewriteCond %{QUERY_STRING} (|%3C).*script.*(|%3E) [NC,OR] |
1 | # Block out any script trying to set a PHP GLOBALS variable via URL |
1 | RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR] |
1 | # Block out any script trying to modify a _REQUEST variable via URL |
1 | RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2}) |
1 | # Send all blocked request to homepage with 403 Forbidden error! |
1 | RewriteRule ^(.*)$ index.php [F,L] |
11. Block access to your .htaccess file
برای جلوگیری از دسترسی user ها به .htaccess ویا سایر انواع فایل از کد زیر استفاده نمایید:
1 | # secure htaccess file |
1 | Files .htaccess |
1 | order allow,deny |
1 | deny from all |
1 | /Files |
1 | # prevent viewing of a specific file |
1 | Files secretfile.jpg |
1 | order allow,deny |
1 | deny from all |
1 | /Files |
1 | # multiple file types |
1 | FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$ |
1 | Order Allow,Deny |
1 | Deny from all |
1 | /FilesMatch |
12. Rename htaccess files
برای تغییر نام .htaccess جهت جلوگیری از تغییرات در آن
1 | AccessFileName htacc.ess |
13. Disable directory browsing
برای فعال یا غیرفعال کردن نمایش لیست زیر محتوای دایرکتوری ها
1 | # disable directory browsing |
1 | Options All -Indexes |
1 | # enable directory browsing |
1 | Options All +Indexes |
14. Change default Index page
برای تغییر صفحه index پیش فرض
1 | DirectoryIndex business.html |
15. Block unwanted visitor based on referring domain
جلوگیری از بازدید کننده از دامنه خاص
1 | # block visitors referred from indicated domains |
1 | IfModule mod_rewrite.c |
1 | RewriteEngine on |
1 | RewriteCond %{HTTP_REFERER} scumbag.com [NC,OR] |
1 | RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR] |
1 | RewriteRule .* - [F] |
1 | /ifModule |
16. Blocking request based on User-Agent Header
ذخیره سازی پهنای باند با محدود سازی برای botها و spiderها
1 | # block visitors referred from indicated domains |
1 | IfModule mod_rewrite.c |
1 | SetEnvIfNoCase ^User-Agent$ |
1 | .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT |
1 | SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT |
1 | Deny from env=HTTP_SAFE_BADBOT |
1 | /ifModule |
17. Secure directories by disabling execution of scripts
امن سازی دایرگتوری ها از اجرای اسکریپت های مخرب
1 | # secure directory by disabling script execution |
1 | AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi |
1 | Options -ExecCGI |
1 دیدگاه