Once we think about URL rewriting, usually meaning adding some rules to the .htaccess file for an Apache world wide web server. If you work using a different server, there’s often documentation designed for translating from Apache’s mod_rewrite guidelines. I even found an programmed converter for nginx.
Rewrite rules form part with the Apache web server’s configuration for just a website, and can be placed in several different locations as part of your respective virtual host configuration. By far the simplest and most portable option using an. htaccess file in your blog root.
The general formula for some sort of rewrite rule is:
RewriteRule URL/to/match URL/to/use/if/it/matches [options]
After we consider about rewriting, have two things:
- redirecting the browser even to another URL;
- Or rewriting the URL internally to train on a particular file.
Redirects match a newly arriving URL, and then redirect the user’s browser completely to address. These can be useful with regard to maintaining legacy URLs if content changes location during a site redesign. Redirecting the old URL to the new location makes certain that any incoming links, such as those from engines like Google, continue to work.
A redirect might look in this way:
RewriteRule ^article/used/to/be/here\. php$ /article/now/lives/here/ [R=301, L]
RewriteRule ^for/this/url/$ /use/this/file. php [L]
We’ve already met two regexp characters.
- ^ Matches the beginning of a string
- $ Matches the end of a string
In this, pattern starts with ^ and ends with $, make sure it match the complete URL initial to end. There are lots of different ways to match.
- [0-9]Matches a number, 0–9. [2-4] would match numbers 2 to 4 inclusive.
- [a-z] Matches lowercase letters a–z
- [A-Z] Matches uppercase letters A–Z
- [a-z0-9] adding some of these, this matches letters a–z and numbers 0–9