Archives

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:

How to show hidden files (.htaccess) in cPanel File Manager

Many users ask us how they can see the .htaccess files for their websites in cPanel File Manager. By default, such files are not shown. To see the hidden files in cPanel File Manager follow these steps:

  1. Log into your cPanel account
  2. Go to File Manager (Files Section)
  3. Click the top-right Settings button.
  4. Then on the Preferences window, check the option Show Hidden Files (dotfiles)
  5. Confirm the action by clicking the Save button
  6. Now you will see all hidden files (including .htaccess files) in File Manager.
Show hidden files cPanel
Show hidden files in cPanel
Notice that many PHP scripts (like WordPress, DokuWiki etc) need the .htaccess files to function properly. Don’t delete these files. Also take care on editing them.

The video tutorial is here:

Share this post:
Page 1 of 2
1 2