1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Support for URL rewriting
URL rewriting on the fly is a technique designed to modify the apparent name of Web pages but where the change is actually achieved simply by virtual redirections from the new (visible) name to the old name (invisible to the visitor). The name of the page does not therefore change per se.
This generally helps to give pages a "cleaner" name, e.g. by hiding URL parameters passed to dynamic web pages. It is then no longer possible for anyone to see that you are using dynamic pages with extended URLs. Not only does it look better for the visitor, but it is of particular benefit when it comes to your pages being indexed by Web searches, as the latter generally exclude URLs with too many parameters.
Actions to be carried out
1. Page name rewriting
We could transform the following Web page URL "article.php?id=25&category=4&page=3" into "article-25-4-3.html", or indeed "title-article-25-4-3.html". Here is the content to place in your .htaccess file to do this, assuming that the article.php file is located in the web/admin/ server directory.
Options +FollowSymlinksRewriteEngine onRewriteBase /admin/RewriteRule ^article-([0-9]*).([0-9]*).([0-9]*).html$ article.php?id=$1&categorie=$2&page=$3 [L]
The "Options +FollowSymlinks" line allows symbolic links to be followed and is not always needed.
The "RewriteEngine on" line enables the Apache mod_rewrite module, i.e. enabling URL rewrites.
The "RewriteBase /admin/" line allows you to specify once and for all the directory to which the following files will apply (article.php, etc).
The "RewriteRule" line, which may seem a little more complex, must be added for every URL you wish to rewrite.
In our example, we have just one line, made up as follows:
The initial "^" character indicates the start of a virtual file name that we wish to redirect.
The "$" character following the ".html" denotes the end of this virtual file name.
Each "([0-9]*)" group is based on regular expressions and indicates that a series of numbers from 0 to 9 will be present and then picked up and placed in the corresponding "$1", "$2" and "$3" variable which can be found at the end of the line.
The "[L]" sign indicates the end of a line, and hence the end of the rewrite rule for this item.
So when a visitor to your site asks for the page "article-25-4-3.html", the .htaccess file will then automatically redirect the request to "article.php?id=25&category=4&page=3" without the user noticing.
Warning: even if your URL rewrite rules are in place and working correctly, it will still be possible to access your pages using the old parametrised URL. Be sure therefore to check that all links to your site have been correctly modified to use the new format.
To create more complex rewrite rules, please refer to the Apache documentation on URL rewrites.
2. Permanent redirection to a second domain
If you own more than one domain for a single website and wish to publish them all, you have the option of redirecting all page requests to another domain, so as to keep your main domain visible in the browser address bar. For example, if "www.mydomain.xyz" and "www.my-domain.xyz" both point to the same website and the former is the main domain, here is the content of the .htaccess file to install in the first site's root directory:
RewriteEngine OnRewriteRule ^(.*)$ http://www.my-domain.xyz/$1 [R=301]
This rule will thus retrieve the name of every page under "www.mydomain.xyz" and open it at "www.my-domain.xyz", completely transparently to the user. [R=301] means permanent redirection.