How To make a Redirection 301

The file htaccess allows to annul certain decisions him that the servant realises in background. What this means for the CATHEDRAL is that we can manipulate the information that the servant back from sends the client of a way that the web search engines and the users find superior.

301 is the code of state HTTP for a permanent redirection. Therefore, a commando 301 invoked by htaccess allows the web search engines to know that any authority/connects accumulated by the old URL must go to the new URL for always. Redirections 301 are the bread and the butter of the URL normalization.

The syntax

Redirection 301 is extremely simple:

Redirect 301 /ruta/archivo.php http://www.yoursite.com/nuevaruta/archivo.php

The necessity of more power

Redirigir pages using the standard commando 301 who is above is extremely simple when it has a small number of pages to consider.

Nevertheless, once the rules become more complex, as a transference of directory or site, the commando redirect 301 becomes extremely inefficient in the time.

To create a redireccionamiento 301 for thousands of pages easily can take days of boring work.

The solution? The tool of reescritura of Apache.

A word on regular expressions

Although the following examples in the rest of the article can modify slightly to adapt to their needs, I recommend to him that it learns regular expressions. They are not as difficult as they seem.

A simple example using the reescritura
Before declaring RewriteRules, it needs to tell him to .htaccess that it ignites the Rewrite Engine.

.htaccess places following near the part the superior of its file: RewriteEngine On the syntax for Rewrite Rules is the following one:

ReWriteRule url_pattern file_reference

[FLAGS]

We are going to define these

Landlord of URL: a landlord of regular expression that will trigger the reescritura

Reference of file: the file that will be

FLAGS: optional improvements as redirection or insensible coincidence to capital letters and small letters

1. To change the file extension

For example, we suppose that his old servant only allows to show simple archives him HTML. Now that has moved to host that it does not use diskettes, it has decided to implement some php.

You had one ton of archives HTML that now are archives php, and does not wish to incur no problem of duplicated content or to lose the connection juice that those pages HTML had accumulated.

The solution:

RewriteBase/
RewriteRule ^ (. *) .HTML$ $1.PHP [R=301, L]

So now all the requests to www.yoursite.com/whatever.html are redirected to www.yoursite.com/whatever.php

Explanation:
We begin with the URL landlord
^ (. *). HTML $

Now section by section:

^ the interleave symbol agrees with the beginning of a chain. In our case, the chain is the URL. Rarely you exclude caret; without the put in one, it can introduce ambiguities.

(. *) the operator of point for regular expressions agrees with any character. The following cuantificador in star represents €œ0 or more instances€. Therefore, * together it produces €œany character, 0 or more times€. By €˜0 or€™, it does not imply more that the character must be repeated:. * It agrees with the cords aaaaa and 23e2323. Any character, any number of times.

The parenthesis around the point and * indicates to the motor that stores (it remembers) these coincident characters in which we called a reference reference (a little as a variable). This allows us (re) to use these characters more ahead, which is extremely useful!

.html $ Sabemos that .html is an extension. Nevertheless, as it were defined previously, the operating point represents any character. Then the landlord €œ.html€ for a motor of regular expressions can be €œ9html€ or €œlhtml€, any thing! When using the escape character, him tenth to the motor that we want, literally, to agree with a point.

The dollar sign agrees with the end of the chain. The combination of the symbol of interleave and the sign of dollar guarantees that our landlord agrees with the complete URL and not only with a subchain of the URL.

Now that we have finished the landlord, we throw a look to the reference of the file.

$1.php the parenthesis in landlord URL helped us to capture all the characters until the .html extension. We can accede to these references through dollar sign.

$1 agrees with the first reference, $2 agrees with the second, and so on.

Now the archives since website.com/file.html makes reference to website.com/file.php

But in this example, we do not want to pause in the reference. If we left the flags in the end, the code as it is simply would show the content of file.php although the URL continues being file.html. That only is contained duplicate!

When placing the flags [R = 301, L] at the end of the reescritura rule, we signal to redirect to file.php and to cause that they reside in the URL.

This can seem a little complex at the outset, but after becoming familiar with regular expressions it will easily include rules of reescritura as this.

2. More reescritura of fragments

Forza www in URL:

RewriteCond% \ {HTTP_HOST}! ^ www.yourwebsite.com [NC] RewriteRule ^ (. *) $ http://www.yourwebsite.com/$1 [R = 301, L]

3. The URL without extensions is due to execute through php.

For example, website.com/work will load website.com/work.php, but the URL will not have the extension (and the URL without extension is sexys)

RewriteRule ^$index.php RewriteRule ^ ((! (. |.php)).) *$ $0.php

4. Redirigir index.php to the options by the root

+FollowSymLinks
DirectoryIndex index.php RewriteCond% \ {THE_REQUEST} ^ 3.9 [AZ] \ {} /index.php HTTP/
RewriteRule ^index.php$ http://www.yourwebsite.com/ [R=301, L]

5. Use of Rewrite With WordPress:

We say that WordPress is using and wishes a beautiful URL to represent books listed by author in a book page. WordPress executes its own rules of reescritura that redirigen everything to index.php, reason why many of their rules of reescritura will not work because probably it is trying to rewrite a URL that already has been rewritten.

The solution consists of using the parameter of name of page GET of index.php.

RewriteRule ^author/(. +) $ index.php? pagename=books&author=$1

So the URL will appear as www.buybooks.com/author/king, but the servant will load the file php as if outside www.buybooks.com/index.php?pagename=books & author=king were entered. This means that it has access to the values through $ _GET in the book page of his subject. Quite useful for a WP developer that wants to organize the information.

The world of the regular expressions is quite fascinating. The reescrituras allow that their sites become organized more And more flexible!

Leave to comment