VARIS VONGUEA-AREE

PHP 🐍 Python 🔥 Golang 🇹🇭 Bangkok Thailand

High level experience in web design and development knowledge, producing quality work.

For more info
  • Home
  • About
  • Project
  • Blog
  • Varisz
  • Web Server
  • การตั้งค่า htaccess Apache Web Server

การตั้งค่า htaccess Apache Web Server

hypertext access เป็นตัวแรกที่จัดการ route ในแต่ละ Directory สามารถมีไฟล์ .htacess ได้ไฟล์เดียว

HTTP Return status ทั้งหมด https://httpstatuses.com/

Combine mergin 2 line https://www.gillmeister-software.com/online-tools/text/merge-lists-line-by-line.aspx

SSL HTTPS

RewriteEngine onRewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]or RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]orRewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

orRewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]</IfModule>

Remove Index?

RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R=301]RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]//Allow Folder File not have index.php
RewriteCond $1 !^(index\.php|assets|images|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]

 

Custom Link

RewriteRule ^content/([^/]*)/([^/]*).html /page.php?content=content&id=$1&name=$2 [L]
RewriteRule ^pages/([^/]*)/([^/]*).html /page.php?content=pages&id=$1&name=$2 [L]
RewriteRule ^news-event/([^/]*)/([^/]*).html /page.php?content=news&id=$1&name=$2 [L]
RewriteRule ^category.html /category.php [L]
RewriteRule ^category/main/([^/]*)/([^/]*).html /category.php?maincate=$1&name=$2 [L]
RewriteRule ^category/sub/([^/]*)/([^/]*).html /category.php?subcate=$1&name=$2 [L]

Redirect

Redirect 301 /tstt /RHINOPLASTY.html
RedirectMatch ^/oldfolder/(.*) http://example.site/newfolder/$1

RewriteRule ^.*[-](\d+)/$ postnewl/index\.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.shtml$ $1.html [L,NC,R=301]

RewriteRule ^news\/categoryfront\.php\/id\/13\/News\.html$ news/categoryfront-php/id/13/News.html [L,R=301]

RewriteCond %{QUERY_STRING} ^cid=14&lid=1043$
RewriteRule ^[/]? test/archive.html/14/1043? [L,R=301]

RewriteCond %{QUERY_STRING} ^cid=([0-9]+)&lid=([0-9]+)$
RewriteRule ^[/]? test/archive.html/%1/%2? [L,R=301]

//http://gas.th/1295dd-2/ to https://test\.com/new
RewriteRule ^1295-2/  https://test\.com/new/ [R=301,L]

GZIP

<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>

Expire Header

# Expires headers (for better cache control)
<IfModule mod_expires.c>ExpiresActive on# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault                          "access plus 7 days"# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest       "access plus 7 days"# Your document html
ExpiresByType text/html                 "access plus 7 days"# Data
ExpiresByType text/xml                  "access plus 7 days"
ExpiresByType application/xml           "access plus 7 days"
ExpiresByType application/json          "access plus 7 days"# Feed
ExpiresByType application/rss+xml       "access plus 7 days"
ExpiresByType application/atom+xml      "access plus 7 days"# Favicon (cannot be renamed)
ExpiresByType image/x-icon              "access plus 1 week"# Media: images, video, audio
ExpiresByType image/gif                 "access plus 7 days"
ExpiresByType image/png                 "access plus 7 days"
ExpiresByType image/jpg                 "access plus 7 dayss"
ExpiresByType image/jpeg                "access plus 7 days"
ExpiresByType video/ogg                 "access plus 7 days"
ExpiresByType audio/ogg                 "access plus 7 days"
ExpiresByType video/mp4                 "access plus 7 days"
ExpiresByType video/webm                "access plus 7 days"# HTC files  (css3pie)
ExpiresByType text/x-component          "access plus 7 days"# Webfonts
ExpiresByType application/x-font-ttf    "access plus 7 days"
ExpiresByType font/opentype             "access plus 7 days"
ExpiresByType application/x-font-woff   "access plus 7 days"
ExpiresByType application/x-font-woff2  "access plus 7 days"
ExpiresByType image/svg+xml             "access plus 7 days"
ExpiresByType application/vnd.ms-fontobject "access plus 7 days"# CSS and JavaScript
ExpiresByType text/css                  "access plus 7 days"
ExpiresByType application/javascript    "access plus 7 days"
ExpiresByType application/x-javascript  "access plus 7 days"
ExpiresByType text/js                   "access plus 7 days"</IfModule>or <IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(gif|jpeg|jpg|png|ico|js|css|swf)$">
ExpiresDefault "access plus 7 days"
</FilesMatch>
</IfModule>

Keep alive

<IfModule mod_headers.c>
Header set Connection keep-alive 

Header unset Pragma
Header unset ETag
FileETag None
</IfModule>

Header Security

<FilesMatch "\.(html|php)$">
Header set Content-Security-Policy "policy-definition
</FilesMatch><IfModule mod_headers.c>Header always set Strict-Transport-Security "max-age=16070400"
Header set X-Content-Type-Options "nosniff"
Header set Referrer-Policy: unsafe-url
Header set X-Frame-Options DENY
Header always set X-Xss-Protection "1; mode=block"
Header always set Feature-Policy "same-origin"
Header set Expect-CT "max-age=7776000, enforce"
Header unset X-Powered-By
Header set Connection keep-alive</IfModule>


https://github.com/phanan/htaccess

© 2021 All rights reserved by varisz