1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Support for URL rewriting
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, assuming that article.php is located in the web/admin/ server directory.
The line "Options +FollowSymlinks" allows symbolic links to be followed and is not always needed.
RewriteRule ^article-([0-9]*).([0-9]*).([0-9]*).html$ article.php?id=$1&category=$2&page=$3 [L]
The line "RewriteEngine on" enables the Apache mod_rewrite module, i.e. enabling URL rewrites.
The line "RewriteBase /admin/" 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 complex, must be added for every URL you wish to rewrite.
In our example here, 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 web site 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.com" and "www.my-domain.com" both point to the same web site and the former is the main domain, here is the content to place in an .htaccess file which you should then install in the first site's root directory:
This rule will thus retrieve the name of every page under "www.mydomain.com" and open it at "www.mon-domaine.com", completely transparently to the user. [R=301] means permanent redirection.
RewriteRule ^(.*)$ http://www.my-domain.com/$1 [R=301]
Link to this FAQ: