My .htaccess Tricks

Berikut beberapa trik .htaccess yang biasa kami pakai, siapa tahu bisa membantu untuk kebutuhan Anda dalam memanage server

Disable Folder/Directory Indexing

Supaya direftory tidak penting tidak terindex Google (menurunkan relevansi website) dan tidak terlihat manusia

#Disable Directory Indexing
Options -Indexes

Resource CDN HTTPS (cdn.example.com) Tidak Muncul

Tambahkan baris ini di .htaccess tersebut

# enable CORS (Cross Origin Resource Sharing)
# reference: http://enable-cors.org/server_apache.html

Header set Access-Control-Allow-Origin "*"

Jangan Proses File Dari Sub Domain Tertentu

# kalau awalannya suatu sub domain tertentu, do nothing
# reference : http://serverfault.com/questions/391843/htaccess-redirect-non-www-to-www-and-retain-subdomains-from-redirecting

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^origin\. [NC]
RewriteRule ^ - [L]
</IfModule>

Redirect dari Non-HTTPS ke HTTPS

# redirect non-https to https
# reference: https://wordpress.org/plugins/really-simple-ssl/

# BEGIN rlrssslReallySimpleSSL rsssl_version[2.2.12]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL

Bisa juga seperti ini

# BEGIN rlrssslReallySimpleSSL rsssl_version[2.2.16]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL

Versi terbaru,

# BEGIN rlrssslReallySimpleSSL rsssl_version[2.3.13]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL

Enable Litespeed Cache

# exclude awalan wp-admin, wp-login.php, wp-cron.php
# reference : http://blog.hawkhost.com/2015/07/22/super-charge-wordpress-with-litespeed-cache/
# exclude request dengan extention php, json, xml
# reference : http://stackoverflow.com/questions/5494542/how-to-exclude-specific-file-types-from-the-following-htaccess-rewrite-rule
# exclude request dengan get paramater (?)

# exclude request dengan get paramater (?)
# reference : http://stackoverflow.com/questions/12264952/exclude-request-with-get-parameter-from-htaccess-rule
# masalah user wordpress gak bisa login setelah litespeed cache nyala, gunakan “/” sebelum rules

# masalah user wordpress gak bisa login setelah litespeed cache nyala, gunakan “/” sebelum rules
# reference : https://www.litespeedtech.com/support/forum/threads/solved-wordpress-users-cant-login-when-litespeed-cache-is-active.9922/

Untuk full wordpress cache,

<IfModule LiteSpeed>
CacheEnable public /
Header set LSCache-Enabled: Yes
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(HEAD|GET)$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-login.php|wp-cron.php)
RewriteCond %{REQUEST_URI} !^/.*/(wp-admin|wp-login.php|wp-cron.php)
RewriteCond %{REQUEST_URI} !.(php|json|xml)$
RewriteCond %{QUERY_STRING} !.+
RewriteRule .* - [E=Cache-Control:max-age=120]
</IfModule>

Untuk static files cache,

<IfModule LiteSpeed>
CacheEnable public /
Header set LSCache-Enabled: Yes
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(HEAD|GET)$
RewriteCond %{REQUEST_FILENAME} \.(css|js|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ [NC]
RewriteRule .* - [E=Cache-Control:max-age=120]
</IfModule>

Jangan Proses File Tertentu

Misalnya file robots.txt

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^robots.txt - [L]
</IfModule>

Menambah Caching Time

Source:  https://varvy.com/pagespeed/leverage-browser-caching.html

## EXPIRES CACHING AND CACHE CONTROL ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
Header set Cache-Control "max-age=2592000, public"
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access 1 day"
ExpiresDefault "access 1 day"
Header set Cache-Control "max-age=86400, public"
</IfModule>
## EXPIRES CACHING AND CACHE CONTROL ##

Redirect File / Folder Yang Kosong Ke Post ID Tertentu

Dari example.net/NUMBER/* ke example.com/?p=NUMBER

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.*)$ http://example.com/?p=$1 [R=301,NC,L]
RewriteRule ^Windows/([0-9]+)/(.*)$ http://example.com/?p=$1 [R=301,NC,L]
RewriteRule ^Android/([0-9]+)/(.*)$ http://example.com/android/?p=$1 [R=301,NC,L]
RewriteRule ^Drivers/([0-9]+)/(.*)$ http://example.com/driver/?p=$1 [R=301,NC,L]
</IfModule>

 Mencegah File Caching Dengan .htaccess

Source: http://www.ipserverone.info/programming/how-to-prevent-http-file-caching-with-htaccess/

<FilesMatch "\.(html|htm|js|css|php)>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</FilesMatch>

Preconnect CDN Melalui HTTP Header

Soource: https://www.igvita.com/2015/08/17/eliminating-roundtrips-with-preconnect/

Header set Link <https://cdn.domain.com/>; rel=preconnect; crossorigin;

Skip 404 Error Handling Oleh WordPress (Static Files)

Source: https://wordpress.org/support/topic/plugin-w3-total-cache-do-not-process-404-errors-for-static-objects-with-word

File .html dan .xml tidak kita return 404 karena dibutuhkan untuk generate dynamic files (page dan sitemap)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} \.(css|js|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ [NC]
    RewriteRule .* - [L]
</IfModule>

Redirect 301 HomePage

Terkadang halaman homepage tidak cached dengan baik oleh WP Super Cache, jadi berapa menit sekali halaman depan cachenya terhapus. Begini cara mengatasinya, kita buat setiap akses ke domain.com/ menjadi domain.com/?home

# 301 Redirect Old File, only homepage (/)
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ /?home [L,R=301]

Block Bad Bots

Supaya bad bots tidak crawling website kita dan menghabiskan resources

Source: https://www.blackhatworld.com/seo/anybody-with-the-code-to-block-majestic-ahrefs-and-all-others-from-crawling-a-site.584470/

SetEnvIfNoCase User-Agent .*rogerbot.* bad_bot
SetEnvIfNoCase User-Agent .*exabot.* bad_bot
SetEnvIfNoCase User-Agent .*mj12bot.* bad_bot
SetEnvIfNoCase User-Agent .*dotbot.* bad_bot
SetEnvIfNoCase User-Agent .*gigabot.* bad_bot
SetEnvIfNoCase User-Agent .*ahrefsbot.* bad_bot
SetEnvIfNoCase User-Agent .*sitebot.* bad_bot
SetEnvIfNoCase User-Agent .*ia_archiver.* bad_bot
<Limit GET POST HEAD>
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</Limit>

 

About Ahli Kompie 298 Articles
Haryo adalah admin AhliKompie.com. Dia merupakan sarjana komputer (teknik informatika) dari salah satu universitas swasta ternama di Jakarta.

Be the first to comment

Leave a Reply

Your email address will not be published.


*