11 Vote

HTACCESS: Simplify URL

Tutorial by Stefan Trost | Last update on 2021-05-10 | Created on 2015-01-04

Maybe you have ever wondered how those website URLs such as example.com/search, example.com/article/1 or example.com/username are realize technically. Finally, there is almost every time running a dynamic PHP script in the background and there were hardly be stored static HTML pages under those names for each user or search query you can reach with that URL.

One solution is HTACCESS with mod_rewrite. This allows redirecting arbitrary URLs without the site visitors noticing anything about it.

Redirect all Requests to Index File

Let's look at a simple example first. For this, we are watching the following lines into a text file named .htaccess which is placed in the root directory of our web space. In addition, the module mod_rewrite must be enabled to make it to work, but this should be the case on most servers today by default.

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)$ index.php?page=$1 [L]

First, we are starting mod_rewrite with "RewriteEngine On". Then we are defining the root directory as basis for all of our rewriting.

In the third line, it will become interesting. In this line, we are defining our first rule for the redirection. We would like to redirect all URLs consisting of the letters a-z (^([a-z]+)$ is a regular expression for this) to index.php with passing the matching string as the parameter "page".

instead example.com/index.php?page=search is called automatically. Within index.php, we can extract the parameter using $_GET['page'] in order to see which page is requested and to be able to react accordingly.

You should have basic knowledge about regular expressions for this redirections. In this case, ^ and $ are indicating the beginning and the end of a string, the string itself must consist only of letters from a to z. At the end, we are referencing the string found in brackets using $1. Therefore, a URL like example.com/page1 would not match the regular expression, so that this URL will not be forwarded.

By the way, the [L] in the statement stands for "last", meaning that no further rules should be considered after this rule has matched.

Make HTML Pages available without Extension

Suppose we have some HTML pages, such as example.com/search.html, example.com/imprint.html and example.com/articles.htm and would like to make them available under the URLs example.com/search, example.com/imprint and example.com/articles. In this case we can use the following.

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)$ $1.html [L]

Again, we are using the regular expression from above, but this time, we are not redirecting all requests to index.php. Instead, we are creating a file name out of the matched string in combination with appending ".html", so that the request is redirected directly to the HTML file with that file extension on our server.

Virtual Directories

Sometimes, you can find virtual directory structures in URLs that are not physically present on the server. This can also be done using the mod_rewrite module. Let's look at some examples.

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)/([0-9]+)$ index.php?a=$1&b=$2 [L]
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?a=$1&b=$2 [L]

These two rules would redirect URLs consisting of two directory levels. The first rule requires a string of letters in the first directory level and a string of numbers in the second level. So, for example, URLs such as example.com/page/1 or example.com/page/2 would match this first rule. The second rule is expecting strings consisting of letters on both directory levels.

Both rules are redirecting the requests to index.php, where the URL matches are available using $_GET['a'] and $_GET['b'].

RewriteEngine On
RewriteBase /
RewriteRule ^article/([a-z0-9]+)$ artciles.php?a=$1 [L]
RewriteRule ^search/([0-9]+)$ search.php?a=$1 [L]

Of course, it is not necessary that each directory level has to be flexible. In this example, for example, we are redirecting all requests such as example.com/article/xyz123 to artciles.php and all requests such as example.com/search/1 to search.php. With this, it is possible, for example, to dynamically create URLs for numbered search result pages or article links.

Beyond

The mod_rewrite module is very powerful, so that I can not go into all of the possibilities and would like to leave it at this groundwork.

If you should have further questions, feel free to leave a comment. If necessary, I am ready to write more tutorials on this subject.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

The Askingbox Search

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.