What is htaccess
.htaccess - is an auxiliary configuration file of the Apache web server. All changes what made in .htaccess do not affect the global server's settings.With .htaccess, you can control access to site files and directories, create links and redirects, remove duplicate pages from search, and much more.
Syntax
“.” - just a point.“*” - character or group of characters that can be present or absent an unlimited number of times.
“?” - character or group of characters that may or may not be present.
“+” - is usually placed before “*”.
“^” - the beginning of a new line.
"$" - the end of the line.
“$N” - number from 0 to 9. Used to refer to the RewriteRule template.
“%N”- number from 0 to 9. Used to refer to the RewriteCond template.
"[]" - square brackets, used for enumeration.
"{}" - curly braces.
“()” - used to highlight groups of characters.
“|” - an alternative choice.
"\" - a slash is needed to escape special characters.
Variables
SERVER_ADMIN | Server owner mailing address specified during installation. |
SERVER_NAME | Server address, such as domain1.ru |
SERVER_ADDR | Web server IP address. |
SERVER_PORT | httpd port. |
SERVER_PROTOCOL | Version HTTP of server protocol. |
SERVER_SOFTWARE | Name of the software running the web server. |
HTTP_USER_AGENT | Information about the visitor's web browser. |
HTTP_REFERER | Address of the first page. |
HTTP_COOKIE | COOKIEs that will be sent by the web browser. |
HTTP_FORWARDED | Address of the page the user came from. |
HTTP_HOST | Web server address, same as SERVER_NAME. |
HTTP_ACCEPT | Customer's information. |
SCRIPT_FILENAME | Path to a specific page or file. |
PATH_INFO | Contains information about the end of the path. |
QUERY_STRING | String passed as a request when working with CGI scripts. |
AUTH_TYPE | Used to determine the type of client identification. |
DOCUMENT_ROOT | Path to the root directory of the web server. |
REMOTE_ADDR | IP visitor. |
REMOTE_HOST | Address of the visitor. |
REMOTE_IDENT | Information about the remote user. |
REMOTE_USER | Similar to REMOTE_IDENT. |
REQUEST_METHOD | Specifies the request type. |
Examples
Redirect from http to httpsRewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,R=301,L]
Simple, 301 redirect to another site:
Redirect 301 / http://newsite.com/
Redirecting the user to the technical page:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^X.X.X.X$
RewriteCond %{REQUEST_URI} !^not-available.html
RewriteRule ^.*$ not-available.html