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

albertc

Newbie
Messages
32
Likes
0
Points
6
The hidden system file .htaccess, while not part of WordPress, has plenty of valuable features for any website, so you better know and be familiar with it. So let's see what it is and how it works, and learn some useful tricks, some little known, we can use in our WordPress installation.

1. What is the .htaccess file?

Apache server software, which works on most of the Web, offers directory settings through files Access Hypertext , and 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 edit. In this regard, note that you should never provide permissions for all the files .htaccess; the most common and grant permissions insurance is 644, which allows universal access to read and write permissions 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 site's root folder.

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

2. Important notes for beginners with .htaccess

As a configuration file, .htaccess is powerful and 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 you must always copy any relevant file and folder of your website, including files .htaccess original, before you get tangled with files Hypertext Access.

It is also essential 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 returning your Web to regular operation.

3. Beware the performance

Directives .htaccess offers 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 each domain directory and load every file .htaccess in each document request. This causes an increase in the processing time of the page and consequently impairs performance.

For low-traffic sites, such performance failures can be invaluable, but they can become a severe problem for high-traffic sites.

So, as a rule of thumb, you should only use files .htaccess when you have access to the central 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 must know each comment line requires its character #, so if you want to add a lot of text, remember to put a # at the beginning of each line. Using only letters, numbers, hyphens, and underscores is usually recommended to avoid possible errors and unwanted calls to the server.

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

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

[L]
Last rule (final rule): tells the server to stop doing a rewrite after processing the previous directive.

[N]
Next (following): tells Apache to rerun the rewrite until all policies have been implemented rewrite.

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

[P]
Proxy tells the server to handle requests by mod_proxy

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

[R]
Redirect (redirect) tells Apache to launch a redirection, making the browser display the rewritten/modified URL.

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

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

[OR]
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.

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

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

[QSA]
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.
("value").

[T = MIME-type]
Mime Type: declares the mime type of resource it addresses.

[]
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; e.g., [zyz] + is regarded as a coincidence of any number of X, Greek, mushrooms, or any combination of these characters.

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

[Az]
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 the preceding character's exact number ( n); for example, x {3} refers to exactly three x.

to {n,}
not specify a number or more of the preceding character; x {3} 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.

()
Is it used to group characters, considering them 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 to a regular expression (regex); for example, start the argument with the previous character.

$
It indicates the end of a test string as a regular expression, for example, the end of 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; for 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 " ... dominio.es. * - [F] ".

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

*
It refers to zero or more 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 - the 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 " domain.com ".

-d
checks whether the string is an existing directory.

-f
checks whether the string is an existing file.

-s
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.

Some servers do not have the " mod_rewrite active "by default. To be active mod_rewrite (basic rewrite) on your site, add 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 you want to have available. Note that for the policy to work FollowSymLinks, they must enable 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, enabling AllowOverride only in the directory or specific directories is essential if necessary. In the following code example, privileges are activated AllowOverride only in a particular 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 htaccess

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, and resources, and, above all, define rules for multiple virtual repeated or single accommodations for one through the file httpd.conf . Then you 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 of the directives of .htaccess is to improve a website's performance, so let's start there.

Improves performance with AllowOverride

Remember, first of all, that you can limit performance slowdowns by activating AllowOverride only in directories where needed. An example you will see is if active AllowOverride throughout your site, you 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 site's root folder and then activate only the directories from the file server configuration where needed. If you did not have access to this file server configuration and privileges, you need AllowOverride not to use this directive:

# Improve performance by disabling allow override
AllowOverride None

Indicating the character set

We can avoid errors in 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 WordPress), add the following directive:

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

Turning off the signing server

This time, we turned off the signature server that could identify the following:

# 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.