تنظیمات وب سرور یکی از موارد همیشه چالش دار بوده( به نظر من ). اگه با وب سروری مثل Apache آشنایی کافی داشته باشید و البته دسترسی لازم به سرور, به راحتی می تونید Apache رو Optimize کنید. ولی در بسیاری از سرور ها یا هاستینگ ها شما تنها به فایل htaccess. دسترسی دارید که به کمک اون می تونید تنظیمات دلخواه رو برای افزایش بهینگی برنامه و وب سرور ایجاد کنید. بریم سراغ این فایل….
اگر به سایت no-www سر بزنید اونجا دلایل کافی برای عدم استفاده از www در url ها به صورت پیش فرض آورده شده, و می تونید مطالعه کنید. ما از دستورات زیر داخل فایل htaccess. برای این کار استفاده می کنیم:
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
برای تنظیم دسترسی به خود وب سایت یا برنامه می تونیم از دستورات زیر استفاده کنیم.همچنین باید دسترسی به خود فایل htaccess. رو محدود کنیم.
order deny,allow allow from all order allow,deny deny from all
یکی از مواردی که هم برای امنیت و هم برای بهینگی اهمیت داره بحث Canonical هستش. Bot ها و یا مرورگر های مختلف ممکنه درخواست های مختلفی رو برای دسترسی به sitemap, favicon, robots.txt با هدف های متفاوتی ارسال کنند. حالا کاری که ما باید بکنیم اینه که تمامی درخواست ها به این فایل ها رو از طریق هر مدل url به فایل های اصلی که معمولا در root directory هستند redirect کنیم.
# CANONICAL ROBOTS.TXT RewriteBase / RewriteCond %{REQUEST_URI} !^/robots.txt$ [NC] RewriteCond %{REQUEST_URI} robots.txt [NC] RewriteRule .* http://sample.ir/robots.txt [R=301,L]
# CANONICAL FAVICONS RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !^/favicon.ico$ [NC] RewriteCond %{REQUEST_URI} /favicon(s)?.?(gif|ico|jpe?g?|png)?$ [NC] RewriteRule (.*) http://sample.ir/images/icon.png [R=301,L] # CANONICAL SITEMAPS RedirectMatch 301 /sitemap.xml$ http://sample.ir/site_map.xml
Error Handling کلا یکی از مواردی بسیار با اهمیت هست. تاثیر این موضوع بر SEO, Security, Readabilty, Extending, etc کاملا مشهود هستش و البته برای کلاس کار برنامه نویس هم بسیار مهمه. دستورات زیر امکان کنترل کردن این خطا ها رو از سمت سرور و یا ناشی از داده های غلط رو به ما می دهد.
( یه تجربه مشترک: هممون Error های هر چند وقت یکبار پرتال نما های دانشگاهی رو دیدیم )
## Custom error messages ErrorDocument 400 http://sample.ir/error.php?n=400 ErrorDocument 401 http://sample.ir/error.php?n=401 ErrorDocument 402 http://sample.ir/error.php?n=402 ErrorDocument 403 http://sample.ir/error.php?n=403 ErrorDocument 404 http://sample.ir/error.php?n=404 ErrorDocument 405 http://sample.ir/error.php?n=405 ErrorDocument 406 http://sample.ir/error.php?n=406 ErrorDocument 407 http://sample.ir/error.php?n=407 ErrorDocument 408 http://sample.ir/error.php?n=408 ErrorDocument 409 http://sample.ir/error.php?n=409 ErrorDocument 410 http://sample.ir/error.php?n=410 ErrorDocument 411 http://sample.ir/error.php?n=411 ErrorDocument 412 http://sample.ir/error.php?n=412 ErrorDocument 413 http://sample.ir/error.php?n=413 ErrorDocument 414 http://sample.ir/error.php?n=414 ErrorDocument 415 http://sample.ir/error.php?n=415 ErrorDocument 416 http://sample.ir/error.php?n=416 ErrorDocument 417 http://sample.ir/error.php?n=417 ErrorDocument 422 http://sample.ir/error.php?n=422 ErrorDocument 423 http://sample.ir/error.php?n=423 ErrorDocument 424 http://sample.ir/error.php?n=424 ErrorDocument 426 http://sample.ir/error.php?n=426 ErrorDocument 500 http://sample.ir/error.php?n=500 ErrorDocument 501 http://sample.ir/error.php?n=501 ErrorDocument 502 http://sample.ir/error.php?n=502 ErrorDocument 503 http://sample.ir/error.php?n=503 ErrorDocument 504 http://sample.ir/error.php?n=504 ErrorDocument 505 http://sample.ir/error.php?n=505 ErrorDocument 506 http://sample.ir/error.php?n=506 ErrorDocument 507 http://sample.ir/error.php?n=507 ErrorDocument 510 http://sample.ir/error.php?n=510
بعد از این قست ما نیاز به یک فایل به نام error.php داریم که اونو به صورت زیر در آوردم:
'Invalid Data',
222 => 'Access Denied!',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
510 => 'Not Extended'
);
$msg = array(
111 => "Sending Data is not Valid",
222 => "URL is not Valid",
400 => "Your browser sent a request that this server could not understand.",
401 => "This server could not verify that you are authorized to access the document requested.",
402 => "The server encountered an internal error or misconfiguration and was unable to complete your request.",
403 => "You don't have permission to access here on this server.",
404 => "We couldn't find that uri on our server, though it's most certainly not your fault.",
405 => "The requested method is not allowed for the URL.",
406 => "An appropriate representation of the requested resource could not be found on this server.",
407 => "An appropriate representation of the requested resource could not be found on this server.",
408 => "Server timeout waiting for the HTTP request from the client.",
409 => "The server encountered an internal error or misconfiguration and was unable to complete your request.",
410 => "The requested resource is no longer available on this server and there is no forwarding address. Please remove all
references to this resource.",
411 => "A request of the requested method GET requires a valid Content-length.",
412 => "The precondition on the request for the URL evaluated to false.",
413 => "The requested resource does not allow request data with GET requests, or the amount of data provided in the request
exceeds the capacity limit.",
414 => "The requested URL's length exceeds the capacity limit for this server.",
415 => "The supplied request data is not in a format acceptable for processing by this resource.",
416 => "Requested Range Not Satisfiable",
417 => "The expectation given in the Expect request-header field could not be met by this server. The client sent Expect:
",
422 => "The server understands the media type of the request entity, but was unable to process the contained instructions.",
423 => "The requested resource is currently locked. The lock must be released or proper identification given before the method can
be applied.",
424 => "The method could not be performed on the resource because the requested action depended on another action and that other
action failed.",
425 => "The server encountered an internal error or misconfiguration and was unable to complete your request.",
426 => "The requested resource can only be retrieved using SSL. Either upgrade your client, or try requesting the page using
https://",
500 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.",
501 => "This type of request method is not supported.",
502 => "The proxy server received an invalid response from an upstream server.",
503 => "The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again
later.",
504 => "The proxy server did not receive a timely response from the upstream server.",
505 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.",
506 => "A variant for the requested resource is itself a negotiable resource. This indicates a configuration
error.",
507 => "The method could not be performed. There is insufficient free space left in your storage allocation.",
510 => "A mandatory extension policy in the request is not accepted by the server for this resource."
);
echo $code[$error];
echo $msg[$error];
?>
برای فعال کردن SSI هم از دستورات زیر استفاده می کنیم.
## Enable SSI AddType text/html .html AddType text/html .shtml AddHandler server-parsed .html AddHandler server-parsed .shtml
اگه شما یک Developer باشید و از افزونه هایی مثل YSlow, Firebug و … استفاده کنید می بینید که به عدم فشرده شدن داده ها و بحث Caching و Expire حسابی گیر می دن. یکی از راه های ساده برای Optimize کردن این موارد استفاده کردن از دستورات زیر هستش.
## Adding an Expires Header Header set Expires "Thu, 15 Apr 2012 20:00:00 GMT" #Compress SetOutputFilter DEFLATE
علاوه بر این که به کمک خود PHP هم می تونیم داده ها رو Compress کنیم, این دستورات وب سرور رو وادار می کنه تا فایل هایی رو که ما در پایین مشخص کردیم به صورت فشرده شده به سمت کاربر ارسال کنه.
# compress text, html, javascript, css, xml: AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
سعی کردم چند تا از مهم ترین موارد رو ذکر کنم.
در مورد منابع هم چند تا سایت مختلف رو معرفی می کنم که احتمالا شما دوستان اون ها رو قبل از من شناختید.
http://www.securityplanet.com
http://www.askapache.com
phpmaster.com
عالی بود
ممنون ..!
ممنون بابت مطلب، ولی از اونجایی که این فای .htaccess هست و نه Htaccess (حروف کوچک و بزرگ تو لینوکس تو اسم فایل مهمن) شاید بد نباشه یه دستی هم به عنوان مطلب بکشی
)
ممنون آقا فرود. از سر عادت حرف اول رو بزرگ نوشته بودم :دی
زنده باد
خدا قوت .. استفاده کردیم
با سلام
مطلب Error Handling بسیار مفید بود
بسیار عالی بود و استفاده کردیم .. این مطالب خیلی برای وبمستر ها مهم هستن لطفا بیشتر از این مقالات مفید قرار دهید باز هم ممنون
فوق العاده.