Apache

Apache URL rewrite example: Rewriting and redirecting URLs with mod_rewrite

In this post, we feature a comprehensive Apache URL rewrite example. This article is part of our Academy Course titled Apache HTTP Server Tutorial.

In this course, we provide a compilation of Apache HTTP Server tutorials that will help you get started with this web server. We cover a wide range of topics, from installing the server and performing a basic configuration, to configuring Virtual Hosts and SSL support. With our straightforward tutorials, you will be able to get your own projects up and running in minimum time. Check it out here!

In Apache mod_rewrite example: Redirecting and rewriting URLs, we introduced you to Perl Compatible Regular Expressions (PCRE) and explained how to integrate them with the mod_rewrite Apache module. These tools allow us to perform URL rewriting and redirecting for a variety of purposes at our convenience.

In this tutorial we will build on these concepts and share other examples of URL rewriting you should have handy as a system administrator.

1. Redirecting to a new resource in the same server

If you have changed the name of a page and want both the old and new URLs to be functional (meaning both lead to the new URL), mod_rewrite allows you to get the job done in a transparent way for end users, who will not notice the difference in their browsers. In this example we will use the example1.com virtual host.

For simplicity, let’s suppose that we had a page named about.html and we renamed it to aboutus.html. Since we do not want users to notice the change, all requests to about.html must be rewritten to aboutus.html.

To accomplish this, we need to insert the following line at the end of the virtual host block for example1.com:

RewriteRule    "^/about\.html$"  "/aboutus.html"

After saving changes to the configuration file, restart Apache and browse to http://example1.com/about.html as seen in Fig. 1:

Apache URL rewrite example: 01rw
Figure 1: Redirecting to a new resource without visible URL change

As you can see, there is no page named about.html in the DocumentRoot of example1.com, but the rewrite rule caused Apache to serve aboutus.html instead. In the address bar the end user will not notice the difference. However, if you use replace the previously added rewrite rule with this one

RewriteRule    "^/about\.html$"  "aboutus.html" [R]

the URL in the address bar WILL change and the request will be logged accordingly, as we can see in Fig. 2:

Apache URL rewrite example: 02rw
Figure 2: Redirecting to a new resource with visible URL change

In this last case, the [R] rewrite flag is used to cause the HTTP redirect to be issued to the browser. The 302 HTTP response indicates that the browser is “told” to make a second request to the new URL. This behavior can be see in Fig. 2 above.

2. Redirecting to a resource moved to another server

If for some reason you need to move a page (or any kind of file, for that matter) to a different server, you will want your users to still be able to access that resource by browsing to the usual URL. This option may come in handy if you need to move a site between servers, or to leverage the available bandwidth of the second system.

For example, let’s say we have a video file named video.mp4 inside the media folder of example1.com, and we want to move it to the DocumentRoot directory of example2.com. Since we are hosting both virtual hosts inside the same machine, we will simply use the mv command (in other situations, you will use scp or sftp to transfer the file) to change the location (see Fig. 3 for details):

Figure 3: Apache URL rewrite example: Moving a video file to a different virtual host
Figure 3: Moving a video file to a different virtual host

To allow users to access video.mp4 when they type http://example1.com/video.mp4, use the following rewrite rule in the virtual host block:

RewriteRule   "^/video\.mp4"  "http://example2.com/$1"  [R,L]

Now save changes, don’t forget to restart Apache, and let’s see what happens in Fig. 4:

Apache URL rewrite example: 04rw
Figure 4: Serving a resource that has been moved to a new server

In Fig. 4 we can see that the request to http://example1.com/video.mp4 was actually handled by example2.com. The combination of the R and L flags in the rewrite rule tells Apache to 1) perform a redirect, and 2) disregard further rules, respectively.

3. Serve browser-dependent content

You can also use mod_rewrite to present a different file as home page based on the end user’s browser. We will use four different files (firefox.html, chrome.html, ie.html, and elinks.html for Mozilla Firefox, Google Chrome, Internet Explorer, and the text-based elinks browsers, respectively) for this. Refer to Fig. 5 for details:

Figure 5: Apache URL rewrite example: Different pages to present depending on the user's browser
Figure 5: Different pages to present depending on the user’s browser

Add the following lines to the configuration file of example2.com inside the virtual host block. Note that this relies on the HTTP_USER_AGENT environment variable, which stores the user-agent string identifying the browser:

RewriteCond "%{HTTP_USER_AGENT}"  ".*Firefox.*"
RewriteRule "^/foo\.html$"         "/firefox.html" [L]
RewriteCond "%{HTTP_USER_AGENT}"  ".*ELinks.*"
RewriteRule "^/scg\.html$"         "/elinks.html" [L]
RewriteCond "%{HTTP_USER_AGENT}"  ".*Chrome.*"
RewriteRule "^/scg\.html$"         "/chrome.html" [L]
RewriteCond "%{HTTP_USER_AGENT}"  ".*Trident.*"
RewriteRule "^/scg\.html$"         "/ie.html" [L]
RewriteRule "^/scg\.html$"         "/index.html" [L]

You will notice that the regular expressions used in the above rewrite rules look for a match inside the user agent string (which can be rather large, such as Mozilla/5.0 (Windows NT 6.1; rv:44.0) Gecko/20100101 Firefox/44.0).

Depending on the browser, a different page will be displayed when the user requests http://example2.com/scg.html (note that this file does not exist in the DocumentRoot of this virtual host). If the browser is neither Firefox, Chrome, IE (identified by the string Trident) or elinks, the request will be redirected to index.html.

In Fig. 6 we see how Apache has returned different pages for each of the four browsers mentioned earlier. We also used w3m (another text-based browser) to illustrate the default action when the user agent string does not match the rewrite conditions (in this case, the usual index.html page is served):

Figure 6: Apache URL rewrite example: Returning different pages depending on the user agent string
Figure 6: Returning different pages depending on the user agent string

Why would you want to use mod_rewrite for this? For example, because some browsers may not be compatible with certain versions of Javascript libraries, or perhaps you want to have a minimal page for text-based browsers.

Summary

In this article we have shared a few extra examples of URL rewriting and redirecting with the intent to give you a glimpse of the capabilities of mod_rewrite. Fortunately, the Apache website provides lots of useful information and examples about the use of this module. With that in mind, the sky is the limit!

We hope that you have found this tutorial useful.

Gabriel Canepa

Gabriel Canepa is a Linux Foundation Certified System Administrator (LFCS-1500-0576-0100) and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work. When he's not typing commands or writing code or articles, he enjoys telling bedtime stories with his wife to his two little daughters and playing with them, the great pleasure of his life.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button