Archives

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 start,restart and stop Apache web server

Here we will list some very simple commands to start, restart and stop the Apache web server. Of course, you should take care about the stop command as your websites will stop working.

For starting Apache use:

root@web [/home/admin]# /sbin/service httpd start
httpd (pid 11858) already running
root@web [/home/admin]#

As you can see, if the Appache (httpd service) is already running the system will let you know.

To restart httpd:

root@web [/home/admin]# /sbin/service httpd restart
root@web [/home/admin]#

To stop Apache server :

root@web [/home/admin]# /sbin/service httpd stop
root@web [/home/admin]#
Share this post:
Page 4 of 4
1 2 3 4