لنتعرف على كل ما يخص ملف htaccess ووردبريس من تعريفه و خطوات انشاءه في الووردبريس و كيفية تحريره باستخدام ال cPanel
ملف .htaccess هو ملف حساس و له تأثير كبير على الموقع، و أي خطأ في كتابة اﻷكواد قد يأدي إلى توقف الموقع بأكمله، لهذا من الضروري عمل نسخة إحتياطية للملف قبل البدأ في التعديل عليه.
ماهو عمل ملف htaccess؟
ينتمي ملف htaccess ووردبريس إلى سيرفر Apache. بحيث يمكن بواسطة هذا الملف التحكم بإدارة إعدادات السيرفرالخاص بموقعك الالكتروني وكذلك تستطيع من خلال استخدامه عمل إلغاء للإعدادات، بالإضافة إلى تفعيل أو وقف عمل الوظائف و الميزات المتعددة.
وكما نلاحظ أن اسم الملف الاساسي يتبع بـ نقطة . وهذا يعني علامة النقطة (.) التي تسبق اسم الملف .htaccess تشير إلى أن هذا الملف ملف مخفي.
خصائص ملف htaccess ووردبريس
- يستخدم هذا الملف من أجل عمل معالجة للروابط الدائمة داخل الصفحات.
- إعادة كتابة عنوان ال URL و إعادة توجيهه.
- منع hotlinking في الصور.
- معالجة أخطاء السيرفر.
- حماية كلمة المرور.
- منع الوصول إلى مجلد wp-content
- منع تصفح الملفات
- تعطيع Hotlinking
- منع وصول عناوين IP معينة
- تمكين كاش المتصفح
- تمكين ضغط zip
- عمل إعادة توجيه من نطاق إلى آخر
في الأسفل ستجد شرح بعض الخصائص السابقة الاهم وكيفية كتابة كل كود لكل منها.
انشاء ملف htaccess ووردبريس
عندما تقوم بتثبيت ووردبريس يتم تثبيت وانشاء هذا الملف تلقائياً , ولكن يمكن حذفه في أي وقت , و المشاكل الشائعة التي تستدعي حذف هذا الملف لربما تتلخص بعضها في :
- وجود خطأ في روابط الموقع الدائمة
- وجود خطأ في قراءة ملفات الموقع
- وجود خطأ أو مشاكل في مسألة ( ملفات التخزين المؤقت ) الكاش في الموقع
- وجود مشاكل في قراءة لغة الموقع الافتراضية
قبل البدء ننصحك بعمل نسخة احتياطية للموقع , اذا كنت لا تعرف كيفية عمل نسخة احتياطية لموقع ووردبرس, راجع الدرس هنا.
ستجد الملف عند الدخول الى روت الاستضافة في حسابك واتباع المسار التالي :
public_html ثم قم بانشاء ملف جديد هنا و قم باعطاؤه المسمى التالي .htaccess
ولاتنسى وضع النقطة -يفضل نسخ الكلمة كما هي و لصقها مكان اسم الملف الذي قمت بانشاءه.
الآن في داخل الملف الذي قمت بانشاءه قم بلصق الكود التالي :
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
بعد ذلك قم بالضغط على حفظ , ثم انتقل الآن الى لوحة تحكم ووردبريس , واضغط على قسم الروابط الدائيمة , في ايقونة اعدادات ووردبريس, وهنا ستجد خيارات منها زر حفظ الكل , بدون تغيير أي خيار آخر اضغط فقط على حفظ الكل.
يمكنك إضاقة أي كود تريده بعد # END WordPress أو قبل # BEGIN WordPress و ذلك لتفادي خلط الاوامر المخصصة مع الاوامر التي يكتبها ووردبريس افتراضيا على ملف .htaccess، قبل البدأ في التعديلات ينصح بعمل نسخة احتياطية للملف.
حماية ملف wp-config.php
<files wp-config.php> order allow,deny deny from all </files>
منع تصفح الملفات
Options All -Indexes
تمكين كاش المتصفح
<IfModule mod_expires.c> ExpiresActive on ExpiresDefault "access plus 1 month" # CSS ExpiresByType text/css "access plus 1 year" # Data interchange ExpiresByType application/json "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType text/xml "access plus 0 seconds" # Favicon (cannot be renamed!) ExpiresByType image/x-icon "access plus 1 week" # HTML components (HTCs) ExpiresByType text/x-component "access plus 1 month" # HTML ExpiresByType text/html "access plus 0 seconds" # JavaScript ExpiresByType application/javascript "access plus 1 year" # Manifest files ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds" ExpiresByType text/cache-manifest "access plus 0 seconds" # Media ExpiresByType audio/ogg "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType video/mp4 "access plus 1 month" ExpiresByType video/ogg "access plus 1 month" ExpiresByType video/webm "access plus 1 month" # Web feeds ExpiresByType application/atom+xml "access plus 1 hour" ExpiresByType application/rss+xml "access plus 1 hour" # Web fonts ExpiresByType application/font-woff2 "access plus 1 month" ExpiresByType application/font-woff "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" </IfModule>
تعطيع Hotlinking
RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www.)?your-domain.com/.*$ [NC] RewriteRule .(gif|jpg)$ http://www.your-domain.com/hotlink.gif [R,L]
منع وصول عناوين IP معينة
order allow,deny deny from xxx.xxx.xx.x allow from all
تمكين ضغط zip
<IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
يمكنك مشاهدة الفيديو التالي لمعرفة خصائص هذا الملف اذا كنت لا تحبذ القراءة