Understanding of PHP .htaccess file
The
.htaccess (Hypertext Access) file is an Apache distributed server configuration
file. You can use the .htaccess file to set server configurations for a
specific directory. This directory can be the root directory of your website or
another subdirectory where the .htaccess file is created in order to enable
extra features for that subdirectory.
You can use
the .htaccess file to modify various configurations and thus make changes to
your website. These changes include authorization, error handling, redirects
for specific URLs, user permissions, etc. Like any other Apache configuration
file, the .htaccess file is read from top to bottom. That is, the above
configurations are performed before those below.
Change the
default start page:
DirectoryIndex
home.html
DirectoryIndex
index.html home.html config.php
Block a specific
IP or range of Ips
Order
Deny,Allow
Deny from
192.206.221.140
Block or
allow ranges of IP addresses
Order
Allow,Deny
Deny from
192.192.*.*
Allow from
all
#301
Redirection
RedirectMatch
301 /oldpage/ /newpage/
RedirectMathc
301 /service/ /service.html
Example
·
Open
index.php file which located at C:\xampp\htdocs folder.
·
Comment
out the selected text like above image.
·
Now need
to create .htaccess file in to htdocs folder and write below code and save it.
RewriteEngine
On
RewriteRule
^$ /myweb.com [L]
·
Myweb.com
is your website folder name and now open localhost.
·
You will
redirect to you myweb.com folder every time.
Save your
directory with help of .htaccess file follow the step.
·
Open
your website folder and create .htaccess and .htpasswd files follow the steps.
·
Write
the following code in .htaccess file inside your website folder.
RewriteEngine
On
AuthName
"Restricted Content"
AuthType
Basic
AuthUserFile
C:/xampp/htdocs/myweb.com/.htpasswd
Require
valid-user
·
Now,
write the following code in .htpasswd file inside your website folder.
sumit:bca
·
Where
sumit is username and password is bca
·
Note: if
website don’t ask for user name and password, please restart your server.
·
The
.htaccess and .htpasswd file inside your website folder.
Catch
Control
·
Put this
line in to .htaccess file.
<FilesMatch
"\.(jpg|jpeg|png|gif|swf)$>
Header set Cache-Control
"max-age=86400, public"
</FilesMatch>
·
Now open
your website and follow the steps.
·
Right
click one white-space and select inspect menu.
·
Click on
network and then press f5 function key.
·
Then
click on one.jpg (your image) image.
·
And find
the Cache-Control "max-age=86400”,
·
86400
means 24 hr
If you don’t
write index.php extension in browser then put the following code to .htaccess.
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteRule
^([^\.]+)$ $1.php [NC,L]
Now type
only index.
Custom
404-page redirection, follow the code and steps.
·
ErrorDocument
404 /myweb.com/myerror.html put this line into .htaccess file.