You get a bonus - 1 coin for daily activity. Now you have 1 coin

Multifunctional rewrite rules on Apache

Lecture



Apache servers have a very good and useful function - mod_rewrite. This server function is often compared to a Swiss knife for working with URLs, as it carries very important features. Some of today's material will seem rather "technical", but in SEO without it in any way.

  • Change URL: write dynamic URLs as readable, literal URLs
  • 301 redirect: tells the browser or search engine that the page has been moved.

These features are very different in their properties and carry different results. The rewrite function handles these tasks in the server’s .conf file or in the .htaccess file of the virtual server.

How does the rewrite function work?

This is the command that you write to one of your server configuration files. Many people write this command to a .htaccess file, which in reality is only responsible for the directory on the server where it lies.

To apply this command to the entire server completely, you need to write this command to the apache2.conf file. However, if the .htaccess file is put in the root of the site, then it will work for the entire site. If you are going to use the .htaccess file, then make sure to add the AllowOverride directive to it.

Each browser request to the server passes through this file. Remember that every time you visit a page of your site, there are several requests to the server: one to HTML, one to each css file, one to each Javascript file and one to each picture.

The requested URL is compared to a regular expression, which is the first mod_rewrite parameter in the .htaccess file. If they match, the second parameter in mod_rewrite is used to create the HTML and is sent back to the browser. This does not necessarily mean that the page is redirected ... for this you need to use a redirect.

Do you really need a redirect? Do not hurry...

Do not confuse 301 redirects with other URL solutions in SEO. All of them are done through mod_rewrite and very, very similar, but perform different tasks.

You have to use 301 redirects when you want to tell the browser or search engine bot "This page is no longer there, go to this new address ... I recently moved it." You:

  • say that the page is no longer there
  • show browser or bot its new address
  • tell the browser or bot that the move is temporary
  • Tell the bot that everything that referred to the old address of the page should refer to the new one, since the content remained the same, it was simply moved.

If you use mod_rewrite to hide the URL , you create a link between the URLs of pages that you want to show to users (in navigation, links in content, site maps, etc.) with the real URLs of these pages that will not be visible to anyone.

This is done in order to translate standard URLs, which consist of category IDs, products and other parameters, into letter URLs consisting of words and carrying information.

Example:

/products/details.asp?pid=1&catid=4

Most likely, in this URL, category 4, product ID is 1. Text, image addresses, and others are stored in the database under number 1, and information on the category is stored in another table. In order to display the page title, description meta tags and keywords, the web server searches this data in the category at number 4 in the database. If you want to display links to other products, then you need to make more queries to the tables in the database where they are stored.

To make this link more "human" it should look something like this:

/ products / necklaces / purple-amethyst-necklace-1

Using this link, you also "pull out" all the parameters of the page from the database, but it already looks more logical and informative. Your site links look beautiful, and they also include keywords.

If you use .htaccess or just 404, the decision to use 301 redirects is simple:

  • if you want to tell the search engine that the page is in a different place, then use 301 redirect
  • if you want to make beautiful URLs and disguise the standard, do not use 301 redirects.

What happens to the weight of links, if you do not use a 301 redirect?

Nothing. You also continue to increase the reference mass and improve internal linking. As long as the search bots see that our page (example: / products / necklaces / purple-amethyst-necklace-11623) still exists, the search engine gets all its content and gives full weight to the link along with the HTTP 200 status (OK ).

Syntax mod_rewrite

Take a simple URL:

according to the rule - ^ oldstuff \ .html $ newstuff.html

Checking that the requested page oldstuff.html exists. If it does, it is redirected to the newstuff.html page to generate it and send it to the user. The search robot or browser still thinks it is on the page oldstuff.html.

Other notes: the ^ sign denotes the beginning of the page name, so the search engine will not confuse, say the page reallyoldstuff.html with our oldstuff.html. The $ sign indicates the end of the file name, so the bot will not confuse our page oldstuff.html with page oldstuff.htmlly.

What about that slash in the middle? This is the first parameter of the regular expression in which each character must match the requested one. What we put behind the \ sign is called character hiding, that is, we do not mean each character individually, but we say that there is a whole set of them.

Now, 301 redirect:

^ oldstuff \ .html $ newstuff.html [R = 301, L]

This is a 301 redirect. This is a redirect, because we use R in brackets. This is exactly 301 redirect, because we put 301 after the equal sign, and if we didn’t register anything after the equal sign, it would be 302 redirect, which means that quite recently we moved the page and do not want the weight to be referenced by it. In 99% of cases, you will use exactly 301 redirects, and not 302.

Two parameters are enclosed in square brackets, separated by a comma. The second parameter "L" means Last, that is, the last. He says that the page in the expression is the same as the requested one, and with subsequent requests in .htaccess, this check can be omitted. In 99% of cases using 301 redirects, you will use the "L" parameter.

In 92% of cases of using operations not related to 301 redirects, you will use the "L" parameter. Why not in 99% of cases?

Sometimes it is useful to use several rewrite rules for a single URL. Suppose you have a number of folders in the first level that you want to perform rewrite operations with.

You also have several folders in these folders with which you want to do these operations too. In the first rule, you can replace the name of the main folder, and then the second rule, which will rename the subfolders to it.

Source URL:

/prods/metal1/necklace-1.htm

According to the first rule of mod_rewrite, the URL can be changed / jewelry-products / instead of / prods /, so we get:

/jewelry-products/metal17/necklace-1.htm

According to the second rule, we replace / gold / instead of / metal17 /:

/jewelry-products/gold/necklace-1.htm

Let's say we have a jewelry catalog with prices and photos called [product ID] .jpg. Such a photo name is very convenient for the programmer and the database, but it is not at all acceptable for SEO. You can use the rules of rewrite to change the name of the images, so that the search bots “love” our images and take them to the index. In our case, the path will be as follows:

/images/necklaces/gold/amethyst-1.jpg
instead of this:

/images/prods/1.jpg

Now, the rewrite rule is not only used to create a redirect or URL conversion.

you need to remember that 301 redirects are concepts that are very related to time, or rather, you can wait 2-3 months until the mirror refreshes the site with a visit, and after a little more time you have to wait for the mirror review.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

seo, smo, monetization, basics of internet marketing

Terms: seo, smo, monetization, basics of internet marketing