Archives

Redirect HTML extension to PHP

It’s very easy to redirect requests to .html pages to .php pages. You will need to add some lines to the .htaccess file. If this file doesn’t exist in your directory then you must create it.

The two lines are:

RewriteEngine On
RewriteRule ^(.*).html$ /$1.php [L,R]

Now, html files will be redirected to php file. E.g. https://domain.com/test.html will be redirected to https://domain.com/test.php

Redirect *.html to *.php

Redirect site to another URL except for a specific directory

You are moving your site to another domain. You want to redirect users to the new domain. But you have a script on a directory that you still want to access on the old domain.

The necessary lines that must be added to your .hatccess file are:

RewriteEngine on
RewriteRule !^(script_directory) https://newdomain.com%{REQUEST_URI} [R=301,L]

If you want to redirect two directories, separate them with | :

RewriteEngine on
RewriteRule !^(directory1|directory2) https://newdomain.com%{REQUEST_URI} [R=301,L]

The code will keep the path – for example olddomain.com/dir1/file1.php will be redirected to newdomain.com/dir1/file1.php

Resources:
RewriteRule Directive

Deny access to .htaccess and other hidden files

There are many security guides that recommend protecting your hidden files (.htaccess, .htpasswd etc.) with .htaccess rules.

So to protect your hidden files, just add this code snippet to your main site .htaccess file.

# Deny access to hidden files - files that start with a dot (.)
<FilesMatch "^\.">
Order allow,deny
Deny from all
</FilesMatch>

If you are using a VPS or dedicated server, you can add this code in the /home/.htaccess file, so it will protect hidden files on all the sites.

Notice that you will still be able to edit the file via FTP/control panel File Manager.

Related articles:
.htaccess articles

Block directory access with .htaccess rules

It’s very simple to block access to a directory. We will use in this article some rules for the .htaccess file. Just add the following lines in your .htaccess file:

RedirectMatch 403 ^/folder_name/?$
ErrorDocument 403 default

The above lines will block folder access but will permit access to resources inside the folder. To block access to the folder and all its content, use:

RewriteRule ^folder_name/?$ - [F,L]
ErrorDocument 403 default


The rule can help in blocking some server attacks. An example from our Apache logs:

     20 /usr/local/apache/domlogs/getimages.domainname.com:191.5.199.250 "POST /admin/
     20 /usr/local/apache/domlogs/getimages.domainname.com:85.101.157.105 "POST /admin/
     20 /usr/local/apache/domlogs/getimages.domainname.com:91.139.203.119 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:187.120.145.248 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:213.166.150.190 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:77.31.75.180 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:91.93.49.135 "POST /admin/
     22 /usr/local/apache/domlogs/getimages.domainname.com:170.84.172.187 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:123.193.236.74 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:27.111.43.138 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:62.240.2.74 "POST /admin/
     24 /usr/local/apache/domlogs/getimages.domainname.com:178.149.203.99 "POST /admin/
     25 /usr/local/apache/domlogs/getimages.domainname.com:114.157.222.182 "POST /admin/
     25 /usr/local/apache/domlogs/getimages.domainname.com:93.86.254.50 "POST /admin/
    327 /usr/local/apache/domlogs/getimages.domainname.com:195.244.36.34 "POST /admin/

In this case, just put the first code snippet in the /home/.htaccess file. The rules in this file will be applied to all domains on the server.

Related articles:
.htaccess articles

How to change the default index page in .htaccess

Many times you may need to use a different file name for the default index page. Most web servers use a list of default index pages, like index.html, index.htm, index.php, index.phtml etc.

You can easily instruct the webserver to load a specific file via the .htaccess file. Just edit the .htaccess file and add the lines:

RewriteEngine on
DirectoryIndex newindex.html

Now, instead of the default index.html, index.php file, the server will load the newindex.html file. To have a list of possible index filenames, use:

RewriteEngine on
DirectoryIndex newindex.html newindex.php newindex2.php

Note that the above syntax is equivalent with:

RewriteEngine on
DirectoryIndex newindex.html
DirectoryIndex newindex.php
DirectoryIndex newindex2.php

The Apache server will serve the first existing file from the list. It will try to load newindex.html. In case it doesn’t find it will look for the next file – newindex.php – and so on.

If none of the resources exist and the Indexes option is set, the server will generate its own listing of the directory.

DirectoryIndex Directive

Notice that the .htaccess file is a hidden file. If you can’t see it in your file manager then you must enable listing of hidden files.
Show Hidden Files in FileZilla
Show Hidden Files in cPanel

References:
DirectoryIndex Directive

Page 1 of 3
1 2 3