All you need to know about .htaccess and a few extra tricks


The hidden system file .htaccess , while not part of WordPress, has plenty of really useful features for any website, so you better know and familiar with it . So let's see what it is, how it works and learn some useful tricks , some little known, we can use to our WordPress installation.

1. What is the .htaccess file?

Apache server software, which works on most of the Web, offering directory settings through files  Access  Hypertext , Hypertext Access in English, better known as file .htaccess to give you an idea of where it comes from the moniker file.

These files .htaccess allow custom settings and site-specific system configuration directives defined in the main configuration file of Apache ( httpd.conf ).

These custom policies can operate within a file called .htaccess . For this, the user must give the file .htaccess file proper permissions to access and / or editing. For that matter, in this regard notes that you should never give permissions for all the file .htaccess , the most common and grant permissions insurance is 644, which allows universal access read and write permission only to the system user.

You should also know that the rules - or directives - of .htaccess affect the parent directory and all subdirectories where it is located. So that your settings are applied to an entire website, you must place the file .htaccess in the root folder of the site.

In addition, you can also create files .htaccess on a specific directory or subdirectory to its rules only apply to it.

2. Important notes for beginners with .htaccess

As a configuration file .htaccess is really powerful, and therefore dangerous if not used properly. Even the slightest syntax error, such as a space to spare, can cause a disastrous performance on the server. So it's important that you always copy of any relevant file and folder of your website, including files .htaccess original, before you get to tangle with files Hypertext Access .

It is also important to check that everything works fine on your website after making any changes to the file. htaccess , however small or irrelevant it may seem. If you find an error undo changes or retrieve the saved copy before them in order to return your web to normal operation.

3. Beware the performance

Directives .htaccess offer directory configurations without accessing the main Apache configuration file ( httpd.conf ). However, for performance and security, whenever possible, you should always use the main configuration file for the policy server.

For example, when a server is configured to process directives .htaccess , Apache should look for in each domain directory and load every file  .htaccess in each request for documents. This causes an increase in the processing time of the page and consequently impair performance.

For low traffic sites such performance failures can be invaluable, but can become a serious problem in high-traffic sites.
So as a rule of thumb, you should only use files .htaccess when you have access to the main configuration file server, that is, the file httpd.conf.

4. Regular expressions (regex) available in .htaccess

A  #  tells the server to ignore the line, not the run. It is used to include comments. You have to know each comment line requires its own character # so if you want to add a lot of text remember to put a # at the beginning of each line. It is usually recommended to use only letters, numbers, hyphens, and underscores, to avoid possible errors unwanted calls to the server.

# This is a comment
# each line must have its own pad
# uses only alphanumeric characters, hyphens (-) or underscores (_)

Forbidden (forbidden) tells the server that returns the client a  403 Forbidden.

Last rule (final rule): tells the server to stop doing rewrite after the previous directive is processed.

Next (next): tells Apache to rerun the rewrite until all policies have been implemented rewrite.

Gone (left) tells the server to deliver the status message  Gone (no longer exists).

Proxy tells the server to handle requests by mod_proxy

Chain (chain) tells the server that chains the current rule with the previous one.

Redirect (redirect) tells Apache to launch a redirection, making the browser displays the URL rewritten / modified.

Case No: define any argument that is associated as unaffected by case-sensitive, that is, as cassettes in sensitive, "NC" = "No Case".

Pass Through (pass through) tells mod_rewrite  to pass the rewritten URL back to Apache for processing again.

Or (o) Specifies a logical "or" linking two expressions so that if one or the other is true the rule associated with it is applied.

No Escape: tells the server that redistributes output without escaping characters.

No Subrequest (without underlying request): tells the server to skip the directive if there are underlying internal requests.

Append Query String: calls the server to add a query string to the end of the expression (URL).

Skip (skip): tells the server that the following "x" skip rules if it finds a match ..

[E = variable: value]
Environmental Variable (environment variable) tells the server to set the environment variable ("variable") to the defined value

[T = MIME-type]
Mime Type: declares the mime type of resource to which it is addressed.

specifies a character class, by which any character within the brackets is considered a match, for example in [xyz], with x, y or z.

[] +
character class in which any combination of elements inside the brackets is considered a match; eg [zyz] + considered a coincidence any number of X, and Greek or mushrooms, or any combination of these characters.

It specifies that is not within a character class; eg [^ xyz] considered a match any character that is neither x, nor and, or z.

a dash (-) between two characters within brackets ([]) defines a range of characters; for example [a-zA-Z] refers to all uppercase and lowercase letters a to z.

to {n}
specify an exact number ( n)  of the preceding character; for example x {3} refers exactly three  x.

to {n,}
not specify a number or more of the preceding character; for example x {3} it refers to three xo more.

to {n, m}
specify a range of numbers, between n and m, the previous character; for example x {3,7} refers to three, four, five, six or seven x.

It is used to group together a group of characters, considering them as a single unit; for example (help)? wordpress will refer to wordpress , either with or without the prefix help .

indicates the beginning of a test string a regular expression (regex); for example, start the argument with the previous character.

It indicates the end of a test string a regular expression; for example, end the argument with the previous character.

optional declares the preceding character; for example  ? cachopo  Cachopos worth to cachopo and while I cachopo (s)?  shall bear on cachopo as Cachopos; eg  x?  refers to any or x.

declares denial; example " ! string "will pay for everything except" chain ".

a dot (or period) indicates any single arbitrary character.

indicates that "no" then rewrite the URL, as in " ... * - [F] ".

It refers to one or more characters of the previous character; for example,  G +  refers to one or more Gs, while simply "+" applies to one or more characters of any kind.

It refers to zero or more of the preceding characters; for example, you can use " . * "as a wildcard.

declares a logical "or" operator; for example,  (x | y)  is true for  x  or  and .

escapes special characters (  $ ^ * |!.  ); for example, uses " \. "to indicate / out a point literally.

It indicates a literal point (escaped).

/ *
Zero or more bars.

. *
zero or more arbitrary characters.

^ $
defines an empty string.

^. * $
the gold standard for any chain worth.

[^ /.]
defines a character that is neither a bar nor a point.

[^ /.] +
defines any numbers or characters containing neither bar nor point.

http: //
this is a literal statement - in this case the literal string "http: //".

^ Domain. *
It defines a chain that begins with the term " domain "probably preceded by any number of characters.

^ Domain \ .com $
define the exact string " ".

checks whether the string is an existing directory.

checks whether the string is an existing file.

file checks if the string contains a value other than zero.
Header codes redirection

301 - Moved Permanently
302 - temporarily moved
403 - Forbidden
404 - Not Found
410 - Gone

5. Basic Directives .htaccess

Activate the basic rewrite

There are servers that do not have the " mod_rewrite active "by default. To be active  mod_rewrite  (basic rewrite) on your site adds the following line once in the .htaccess file in the root folder of your website:

# Enable basic rewrite
 RewriteEngine on

Enable symbolic links

Active symbolic links ( symlinks ) by adding the following directive to the file .htaccess in which you want to have available. Note that for the policy to work  FollowSymLinks  they must be enabled privileges  AllowOverride Options  in the server configuration file:

# Enable symlinks
 Options + FollowSymLinks

Activate AllowOverride

To work directives requiring   AllowOverride as  FollowSymLinks , the following directive should be added to the file server configuration. For performance it is important to enable  AllowOverride  only in the directory or specific directories to be necessary. In the following code example privileges are activated   AllowOverride  only in a specific directory (/ www / replace / it / to / the / directory / real). :

# Active privileges AllowOverride
 <Directory / www / replace / it / to / the / directory / Real>
 AllowOverride Options
 </ Directory>

Rename the .htaccess file

Not all systems support only format file extension .htaccess . Fortunately you can rename it to anything you want if the name is valid on your system. This directive should be placed in the configuration file server or will not work:

# Rename files
 AccessFileName ht.access

Note: If you rename the files .htaccess remember to update any adjustment associated with the previous names. For example, if you're protecting your file .htaccess with  FilesMatch , remember to inform the new names:

# Protect renowned htaccess
 <FilesMatch "^ ht \.">
 Order deny, allow
 Deny from all
 </ FilesMatch>

Take advantage of the rules defined in httpd.conf

You can save time, effort, resources and, above all, defining rules for multiple virtual repeated or single accommodations for one through the file httpd.conf . Then you just have to indicate the files .htaccess affected inherit rules httpd.conf including this directive:

Inherit RewriteOptions

6. Improve the performance of your website with .htaccess

One of the most common, and grateful uses the directives of  .htaccess is to improve the performance of a website, so let's start there.

Improves performance with AllowOverride

Remember first of all that can limit performance slowdowns activating   AllowOverride  only in directories where needed. An example you will see very clear it is that if active  AllowOverride  throughout your site should review each directory server, searching for files .htaccess that may not exist. To avoid this we must turn  AllowOverride  in the file  .htaccess in the root folder of the site and then activate only the directories where needed from the file server configuration. If you did not have access to this file server configuration and privileges you need  AllowOverride  not use this directive:

# Improve performance by disabling allow override
 AllowOverride None

Indicating the character set

We can avoid errors parameter 500 indicating the default character set before loading. Of course, replace the "utf-8" example if you use other character sets on your site:

# Indicate the default character set
 utf-8 AddDefaultCharset

Saving bandwidth

To increase performance on servers with PHP active (that is, in which all have a WordPress) adds the following directive:

# Saves bandwidth on servers with PHP active
 <IfModule mod_php4.c>
 php_value zlib.output_compression 16386
 </ IfModule>

Disabling the signing server

This time we disable the signature server that could identify:

# Server signing off
 ServerSignature Off

Setting the time zone of the server

With this directive we tell the server that synchronizes chronologically according to the time zone of the location we have told you:

# Establish the server time zone
 TZ SetEnv Spain / Madrid

Members online

No members online now.