The .htaccess file


Apache Web Server only

The .htaccess file is located in the root folder and performs various useful functions, some related to performance, some to security, some to cleaner urls. That is why Apache Web Server is recommended for Qwwwik. The system will still operate without .htaccess but without some of those benefits. Most shared web hosting uses Apache.

An .htaccess file is installed with Qwwwik whether the server is Apache or not. If not, the file will have no effect.


Default .htaccess file

See a full explanation of the default .htaccess file »

Lines shown in green beginning with a # hash symbol are 'commented out' and have no effect. If required they can be removed altogether. The active lines are only those not beginning with a # hash symbol (not in green).

# BEGIN Qwwwik
<IfModule mod_rewrite.c>
  RewriteEngine on
# Forbid direct viewing of txt files in pages folder
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)/(pages|visits)/(.*)\.txt [NC]
  RewriteRule ^ "-" [F]
# Rewrite non php URLs to php on server
# php URLs still usable
#     is not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
#     is a php file
  RewriteCond %{REQUEST_FILENAME}\.php -f
# Internally rewrite to actual php file
  RewriteRule ^(.*)$ $1.php
# If not found, relative path to error 404 file
#     is not an actual file or directory
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule .* inc/404.php [L]
</IfModule>
# END Qwwwik

# BEGIN GZIP
#
# END GZIP

Aside from performance and security, the main difference between an installation with a functioning .htaccess file and one without is that the addresses of public pages do not need the .php file extension at the end.

Web pages addresses can then be either of:

https://example.com/page
https://example.com/page.php

Internal links can be either of:

<a href="./page">page</a>
<a href="./page.php">page</a>

Addresses without .php at the end look better but a website with links that do not have the extension will not be transferable to a Windows or NGINX server without changing all the internal links to add the extension. This is an unlikely eventuality but is worth bearing in mind.


Extended .htaccess file

Qwwwik has an additional admin page that allows an extended .htaccess file to be created just by pressing a button.

Example:

# BEGIN Qwwwik
<IfModule mod_rewrite.c>
  RewriteEngine on

# EXTERNAL REDIRECTS

# Remove index php root only
  RewriteCond %{REQUEST_URI} !(admin|diagnostics|visits) [NC]
  RewriteRule ^index\.php$ https://domain.com/ [R=301,L]

# Remove php extensions root only
  RewriteCond %{REQUEST_URI} !(admin|diagnostics|visits) [NC]
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
  RewriteRule ^(.+)\.php$ https://domain.com/$1 [R=301,L]

# Hostname canonicalization redirect 1 not www
  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteRule (.*) https://domain.com/$1 [R=301,L]

# Hostname canonicalization redirect 2
  RewriteCond %{HTTPS} !on
  RewriteRule (.*) https://domain.com/$1 [R=301,L]

# FACEBOOK QUERY STRING

# Redirect Facebook query string to non-query string
  RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
  RewriteCond %1%2 (^|&)([^&].*|$)
  RewriteRule ^(.*) /$1?%2 [R=301,L]

# INTERNAL REWRITES

# Forbid direct viewing txt files in pages folder
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)/(pages|visits)/(.*)\.txt [NC]
  RewriteRule ^ "-" [F]

# Rewrite non php URLs to php on server
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php [L]

# If not found then relative path to error 404 file
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule .* inc/404.php [L]

</IfModule>
# END Qwwwik

# BEGIN GZIP
#
# END GZIP

The rules are enclosed by BEGIN and END comments as follows:

# BEGIN Qwwwik
  rules
# END Qwwwik


GZIP compression

If the server has GZIP enabled it can serve parts of a website in compressed form so it downloads faster. Qwwwik's /admin/.htaccess provides an option to add GZIP compression when creating an 'original' or 'extended' .htaccess file. This compresses plain text, all html, the style sheets and javascript. The following can be added or subtracted as desired:

# BEGIN GZIP
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
</IfModule>
# END GZIP

The rules are enclosed by BEGIN and END comments as follows:

# BEGIN GZIP
  rules
# END GZIP

This website has GZIP compression enabled.


Custom rules

Additional custom rules can be written above and below the comments and will not be touched whenever an updated .htaccess file is created in admin, only the rules between the comments.

If you edit the .htaccess file manually, make sure not to remove the BEGIN and END comments. If you do, the file can't be updated in admin. The root folder of the cms contains a file named htaccess.txt which contains the default rules for .htaccess (they are also in this page – top).

See Information

Page last modified: 22 May, 2024
Search | Legal | Qwwwik

Info