Archives

Maintenance mode via .htaccess

Many times we are asked by our clients how to put the site offline but still be able to work on it. We will show in this article how to set a “maintenance mode”/ “down for maintenance” page.

WordPress users can simply use a plugin. Just search, install and test the one that suits your needs. Look at https://wordpress.org/plugins/search/maintenance+mode/

We discuss how you can ‘set’ maintenance mode via the .htaccess file. Notice that .htaccess file is a hidden file. To see hidden files in cPanel, check these articles:
View hidden files in FileZilla
Show hidden files in cPanel

Site Maintenance Mode

The following code snippet will send all web traffic to the maintenance.html file, excepting the traffic from the specified IP address. Replace 123.123.123.123 with your IP address.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ https://domain.com/maintenance.html [L]
</IfModule>

If you are using images, css/js files in the maintenance.html, put them in the /resources/ directory and use:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.123.123.123

RewriteCond %{REQUEST_URI} !^/resources/.*$
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ https://domain.com/maintenance.html [L]
</IfModule>

Some recommend using a redirect code 307 or 503 in the last rule. We do not recommend such a code, as 503 HTTP means Service Unavailable. The 307 HTTP code is Temporary Redirect.

Resources:
HTTP status codes

Share this post:

How to set up a redirect for your website in cPanel

Did yo move your site to a new domain? You want to redirect accesses to a removed folder to the main page? the cPanel Redirect allows you to set such redirects.

To setup a redirect follow the steps:

  1. First, log into your cPanel account
  2. Look for Redirects (DOMAINS section)
  3. Now we will add the redirect. Choose the Type you want Permanent(HTTP code 301) or Temporary(HTTP code 302)
  4. Choose if the redirect should work for all the domains or for a single domain(select it from the drop-down list)
  5. Then select the directory or file you want to redirect from.
  6. Enter the destination URL.
  7. Choose if you want to redirect with/without/and www. Also, you have a Wild Card Redirect option. With this option checked the server will redirect all files within a directory to the same filenames in the destination directory.
  8. Click the Add button
cPanel Redirects
cPanel Redirects

A short video tutorial:

The cPanel Redirect tool uses the .htaccess file. Take care if you want to edit/delete this file. For advanced info on the directives you can use in the .htaccess file, please visit Apache Module mod_rewrite page.
Share this post:

How to remove .php, .html extensions with .htaccess

Many times you will want to have user-friendly URLs on your site. Instead of https://www.example.com/index.html you will want to have https://www.example.com/index . The second URL looks much better. Also, from the SEO point of view is better to don’t use file extensions.

 The steps to remove file extensions are:

  1. Login to your cPanel account.
  2. Go to File Manager – in the FILES Section
  3. In the File Manager go to the Settings button on the top right corner.
  4. On the Preferences window that will appear check the Show Hidden Files (dotfiles) option. Click Save button to apply the settings.
  5. Now navigate to the .htaccess file. If the file doesn’t exist you will need to create it.
  6. Click the Edit button from the File Manager top menu.
  7. Add the below lines to the .htaccess file. Click the Save Changes button and then the Close button. 
    #remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    
  8. Now the Apache web server will remove .php extension from URLs.

To remove .html extension use:

#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Notice that many PHP scripts (like WordPress, Drupal, MediaWiki etc) need the .htaccess files to function properly. Don’t delete these files. Also take care when editing them.

Check the video tutorial we made:

You can also edit .htaccess files on your web hosting account via an FTP client like FileZilla.
Share this post:

How to force www or non-www URLs for your domain via .htaccess in cPanel

To redirect URLs on your site you can use the .htaccess files. We will show you below how to redirect non-www URLs to www URLs and vice versa.
To see more info on how to edit .htaccess files via cPanel please check this Knowledge Article.

htaccess nonwww to www
.htaccess redirect nonwww to www

To force URLs to www version use:

# Redirect non-www URLs to www URLs - like http://yourdomain.com to http://www.yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

To force URLs to non-www version use:

# Redirect www URLs to non-www URLs - like http://www.yourdomain.com to http://yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
Notice that many PHP scripts (like WordPress, Drupal, MediaWiki etc) use the .htaccess files to function properly. Don’t delete these files. Also take care when editing them.
Share this post:

How to deny access to a specific file on your site via .htaccess

Do you want to keep a file on your web hosting account private? You can do so by blocking access to it via a rule in the .htaccess file. The steps are:

  1. Login to your cPanel account.
  2. Go to File Manager – in the FILES Section
  3. In the File Manager go to the Settings button on the top right corner.
  4. On the Preferences window that will appear check the Show Hidden Files (dotfiles) option. Click Save button to apply the settings.
  5. Now navigate to the .htaccess file. If the file doesn’t exist you will need to create it.
  6. Click the Edit button from the File Manager top menu.
  7. Add the below lines to the .htaccess file. Click the Save Changes button and then the Close button. 
    #block access to the file blockedfile.html; permit access to all other files
    <files blockedfile.html>
    Order Allow,Deny
    Deny from all
    </files>
    
  8. Now access to the file blockedfile.html will be blocked. Users will see a 403 Forbidden error.

To block access to specific files (like images), use these lines instead:

#block access to image files - files with jpg/gif/png/jpeg extensions
<FilesMatch "\.(jpg|gif|png|jpeg)$">
Order Allow,Deny 
Deny from all 
</FilesMatc>
Notice that many PHP scripts (like WordPress, Drupal, MediaWiki etc) need the .htaccess files to function properly. Don’t delete these files. Also take care when editing them.

Check the video tutorial we made:

You can also edit .htaccess files on your web hosting account via an FTP client like FileZilla.
Share this post:
Page 1 of 2
1 2