{"id":827,"date":"2016-02-29T17:11:21","date_gmt":"2016-02-29T15:11:21","guid":{"rendered":"http:\/\/www.systemcodegeeks.com\/?p=827"},"modified":"2018-05-31T15:33:26","modified_gmt":"2018-05-31T12:33:26","slug":"apache-mod_rewrite-example","status":"publish","type":"post","link":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/","title":{"rendered":"Apache mod_rewrite example: Redirecting and rewriting URLs"},"content":{"rendered":"<p><em>In this post, we feature a comprehensive Apache mod_rewrite example. This article is part of our Academy Course titled <a href=\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-http-server-tutorial\/\">Apache HTTP Server Tutorial<\/a>.<\/em><\/p>\n<p>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 <a href=\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-http-server-tutorial\/\">here<\/a>!<\/p>\n<p>In <a href=\"http:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-configuration-tutorial\/\" target=\"_blank\" rel=\"noopener\">a previous article<\/a> we mentioned that one of Apache\u2019s most distinguishing features is its extensibility via modules, which we defined as &#8220;independent, separate pieces of software that provide specific functionality&#8221;.<\/p>\n<p>Some modules are built-in into Apache as part of the core functionality and are present when the web server is installed as we explained in \u201c<a href=\"http:\/\/www.systemcodegeeks.com\/web-servers\/apache\/how-to-install-the-apache-web-server\/\" target=\"_blank\" rel=\"noopener\">How to install the Apache web server<\/a>\u201d.<\/p>\n<p>Others, such as mod_bw (which we covered in \u201c<a href=\"http:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-name-based-virtual-host-configuration-example\/\" target=\"_blank\" rel=\"noopener\">Apache name-based Virtual Host Configuration Example<\/a>\u201d), can be installed using your distribution\u2019s package management system.<\/p>\n<div class=\"tip\">The current list of all of the modules that come as part of the Apache HTTP Server can be found at <a href=\"http:\/\/httpd.apache.org\/docs\/current\/mod\/\" target=\"_blank\" rel=\"noopener\">http:\/\/httpd.apache.org\/docs\/current\/mod\/<\/a><\/div>\n<p>[ulp id=&#8217;euI76dOod8fb2P3K&#8217;]<\/p>\n<p>In this article we will explain how to use mod_rewrite (a well-known and widely used module) to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s or virtual host internal structure, or to another external URL.<\/p>\n<p>In other words, this module will allow you to redirect (as the <b><i>rewrite <\/i><\/b>in the name suggests) an URL (<em>http:\/\/www.example.com\/scg\/results.php?country=Argentina&amp;province=Cordoba<\/em>) to a more user or SEO friendly URL (<em>http:\/\/www.example.com\/scg\/Argentina\/Cordoba<\/em>) in order to get a higher position in search engines rankings, which ultimately leads to more visitors.<\/p>\n<p>To accomplish this purpose, mod_rewrite realies heavily on <strong>PCRE<\/strong> (Perl Compatible Regular Expressions) vocabulary, which we will introduce next. Please be advised, however, that this topic can be a little burdensome until you start reaping the benefits out of it.<\/p>\n<h2>1. Introducing regular expressions (regexs)<\/h2>\n<p>In simple words, a regular expression is a text string that represents a search pattern. The following list, adapted from the Apache documentation on PCRE, shows the most common characters used in regular expressions, their meaning, and an example:<\/p>\n<ul>\n<li><b>.<\/b> (a dot) matches any single character. Thus, <b>b.t<\/b> will match b<b>a<\/b>t, b<b>e<\/b>t, b<b>i<\/b>t, b<b>o<\/b>t, and b<b>u<\/b>t.<\/li>\n<li><b>+<\/b> (the plus sign) repeats the previous characters one or more times. For example, o+ matches oo, ooo, etc.<\/li>\n<li><b>* <\/b>(star) repeats the previous match zero or more times. Additionally, * is also used to match an empty string. In other words, the matches returned by a+ are a subset of the matches of a*.<\/li>\n<li><b>?<\/b> (question sign) makes the match of the previous characters optional, so colou?r will match both color and colour.<\/li>\n<li><b>^<\/b> (caret) matches the beginning of the string. For example ^a matches a string that begins with a.<\/li>\n<li>$ (dollar sign) matches the end of the string, so a$ matches a string that ends with a.<\/li>\n<\/ul>\n<p>In addition, you can also group characters into groups of classes:<\/p>\n<ul>\n<li>A set of parentheses <b>( )<\/b> is used to group several characters into a single unit. You can then apply the above regex characters to the group as it was a single character. Thus, (ab)+ matches abab, ababab, etc. Keep in mind that the + here applies to the group of characters surrounded by parentheses.<\/li>\n<li>A character class <b>[ ]<\/b> matches at least one of the characters in the set inside square brackets. For example, [alnum] matches any letter of the alphabet or numerical digit. Character classes are well explained in the <a href=\"http:\/\/php.net\/manual\/en\/regexp.reference.character-classes.php\" target=\"_blank\" rel=\"noopener\">PCRE regex syntax for PHP<\/a>.<\/li>\n<li>On the opposite, a negative character class [^ ] matches any character not specified. Thus, c[^\/]t matches <b>cat <\/b>or <b>c2t<\/b> but not <b>c\/t<\/b>.<\/li>\n<\/ul>\n<p>Finally, you can use the exclamation sign (!) to negate it.<\/p>\n<p>Now we are ready to discuss the <strong>RewriteRule<\/strong> and <strong>RewriteCond<\/strong> directives, which are essential to the operation of mod_rewrite.<\/p>\n<h2>2. Introducing RewriteRule and RewriteCond<\/h2>\n<p>The <b>RewriteRule <\/b>directive, as its name implies, substitutes a given pattern with either one of three things:<\/p>\n<ul>\n<li>An absolute path to a local resource found inside the system<\/li>\n<li>A path to a local web resource<\/li>\n<li>An absolute URL<\/li>\n<\/ul>\n<p>Its basic syntax is:<\/p>\n<pre class=\"brush:bash\">RewriteRule [Pattern] [Substitution] [Optional \u2192 Flags]<\/pre>\n<div class=\"tip\">Since the [Flags] parameters is optional, we will not cover it in detail, but only mention it when we use it in an example. You can find the full [Flags] documentation <a href=\"http:\/\/httpd.apache.org\/docs\/current\/rewrite\/flags.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/div>\n<p>Last, but not least, we need to mention that you can place this directive inside the main configuration file, inside a Virtual host definition or Directory block. You can use multiple <strong>RewriteRule<\/strong> directives in the same context, each one with its own [Pattern], [Substitution] and (optionally) [Flags].<\/p>\n<div class=\"tip\">Alternatively, you can insert RewriteRule directives in .htaccess files. Although this is a widely used but not very secure method, we have chosen to not cover it during this series. However, you can learn more about it in the Apache documentation <a href=\"https:\/\/httpd.apache.org\/docs\/2.4\/howto\/htaccess.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/div>\n<p>The <b>RewriteCond <\/b>directive introduces a condition that must be met before the <strong>RewriteRule<\/strong> rules are \u201cactivated\u201d.<\/p>\n<p>To better understand this topic, let\u2019s illustrate with a few examples.<\/p>\n<h2>3. Examples<\/h2>\n<p>Before we proceed with some examples, there are some considerations that we must take into account. In order to actually use mod_rewrite, we need to add the directives listed below in the context where we will use this module. Additionally, we need to make sure the module is loaded. We do this by running (in Ubuntu)<\/p>\n<pre class=\"brush:bash\">sudo a2enmod rewrite<\/pre>\n<p>In CentOS, it is enabled by default, which you can confirm with<\/p>\n<pre class=\"brush:bash\">httpd -M | grep rewrite<\/pre>\n<p>You should get the following output:<\/p>\n<pre class=\"brush:bash\">rewrite_module (shared)<\/pre>\n<p>If not, you will need to check the presence of the module file (mod_rewrite.so) in \/etc\/httpd\/modules and make sure Apache is loading the modules in that directory. Look for the following line in the main configuration file:<\/p>\n<pre class=\"brush:bash\">Include conf.modules.d\/*.conf<\/pre>\n<p>(By the way, \/etc\/httpd\/modules is actually a symbolic link to \/etc\/httpd\/conf.modules.d)<\/p>\n<p>If it is not there, add it before proceeding.<\/p>\n<p>Suppose we want to enable mod_rewrite in the context of <em>www.example1.com<\/em>. We need to modify its configuration file and add <b>RewriteEngine <\/b>(to enable the rewriting engine). Additionally, you need to set the <b>Options <\/b>directive to allow FollowSymlinks:<\/p>\n<pre class=\"brush:xml\">&lt;VirtualHost *:80&gt;\r\nDocumentRoot \"\/var\/www\/example1.com\/public_html\/\"\r\nServerName www.example1.com\r\nServerAlias example1.com\r\nErrorLog \/var\/www\/example1.com\/error.log\r\nLogLevel info\r\nCustomLog \/var\/www\/example1.com\/access.log combined\r\nBandwidthModule On\r\nForceBandWidthModule On\r\nBandwidth all 20480\r\nMinBandwidth all -1\r\nMaxConnection all 5\r\n&lt;Directory \"\/var\/www\/example1.com\/public_html\/media\"&gt;\r\nLargeFileLimit * 1024 10240\r\n&lt;\/Directory&gt;\r\nRewriteEngine on\r\nOptions FollowSymLinks\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>With that in place, also add the following lines inside the virtual host definition given above:<\/p>\n<pre class=\"brush:bash\">RewriteCond \"%{REMOTE_ADDR}\" \"^192\\.168\\.0\\.104\"\r\nRewriteRule \"^\/vhosterrors\" \"\/var\/www\/example1.com\/error.log\"\r\nRewriteRule \"^\/default\\.aspx$\" \"index.html\" [R]\r\nRewriteRule \"^\/go\/to\/example2$\" \"http:\/\/example2.com\" [R]\r\nRewriteRule \"^\/writer\/(.*)\/view$\" \"\/var\/www\/example1.com\/$1\"\r\n<\/pre>\n<p>(Make sure your configuration is similar to that shown in Fig. 1)<\/p>\n<figure id=\"attachment_830\" aria-describedby=\"caption-attachment-830\" style=\"width: 566px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-830 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/01part4.png\" alt=\"Figure 1: Apache mod_rewrite example: Adding RewriteCond and RewriteRule directives\" width=\"566\" height=\"203\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/01part4.png 566w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/01part4-300x108.png 300w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/><figcaption id=\"caption-attachment-830\" class=\"wp-caption-text\">Figure 1: Adding RewriteCond and RewriteRule directives<\/figcaption><\/figure>\n<p>Let\u2019s see what is happening here:<\/p>\n<pre class=\"brush:bash\">RewriteCond \"%{REMOTE_ADDR}\" \"^192\\.168\\.0\\.104\"<\/pre>\n<p>indicates that the below rules apply if the remote address is 192.168.0.104<\/p>\n<pre class=\"brush:bash\">RewriteRule \"^\/vhosterrors\" \"\/var\/www\/example1.com\/error.log\"<\/pre>\n<p>If you browse to <a href=\"http:\/\/example1.com\/vhosterrors\">http:\/\/example1.com\/vhosterrors<\/a>, a non-existent directory (note that it is a regular expression beginning with the word vhosterrors, as indicated by the caret sign), you will view the error log for the virtual host (\/var\/www\/example1.com\/error.log), as seen in Fig. 2.<\/p>\n<figure id=\"attachment_831\" aria-describedby=\"caption-attachment-831\" style=\"width: 787px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-831 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/02part4.png\" alt=\"Figure 2: Apache mod_rewrite example: Rewriting an URL to point to a local resource\" width=\"787\" height=\"304\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/02part4.png 787w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/02part4-300x116.png 300w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/02part4-768x297.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><figcaption id=\"caption-attachment-831\" class=\"wp-caption-text\">Figure 2: Rewriting an URL to point to a local resource<\/figcaption><\/figure>\n<pre class=\"brush:bash\">RewriteRule \"^\/default\\.aspx$\" \"index.html\"<\/pre>\n<p>If you go to <em>http:\/\/example1.com\/default.aspx<\/em>, you will be taken to the index.html of the virtual host. Refer to Fig. 3 for details.<\/p>\n<figure id=\"attachment_832\" aria-describedby=\"caption-attachment-832\" style=\"width: 427px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-832 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/03part4.png\" alt=\"Figure 3:Apache mod_rewrite example: Another example of URL rewriting\" width=\"427\" height=\"321\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/03part4.png 427w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/03part4-300x226.png 300w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><figcaption id=\"caption-attachment-832\" class=\"wp-caption-text\">Figure 3: Another example of URL rewriting<\/figcaption><\/figure>\n<pre class=\"brush:bash\">RewriteRule \"^\/go\/to\/example2$\" \"http:\/\/example2.com\" [R]<\/pre>\n<p>Browse to <a href=\"http:\/\/example1.com\/go\/to\/example2\">http:\/\/example1.com\/go\/to\/example2<\/a> and you will be redirected to <a href=\"http:\/\/example2.com\">http:\/\/example2.com<\/a>. By the way, the <b>R<\/b> inside square brackets stands for <b>Redirect<\/b>. This rule, as opposed to the previous one (which does a URL rewrite in the full sense of the word), performs a redirect to an external site. You may want to keep in mind that example1.com and example2.com are two different, separate sites even though they are hosted in the same machine.<\/p>\n<p>Finally,<\/p>\n<pre class=\"brush:bash\">RewriteRule \"^\/writer\/(.*)\/view$\" \"\/var\/www\/example1.com\/$1\"<\/pre>\n<p>says that if you go to <a href=\"http:\/\/example1.com\/writer\/gabriel\/view\">http:\/\/example1.com\/writer\/gabriel\/view<\/a>, you will be taken to \/var\/www\/example1.com\/public_html\/gabriel. Here the $1 is a placeholder for whatever matches the regular expression (.*). As explained earlier, the dot stands for any character, and the star sign represents zero or more occurrences of such character. In other words, that is the regular expression for <b>match everything<\/b>. Since this file does not exist, in Fig. 4 we can see a portion of the error log that says so:<\/p>\n<figure id=\"attachment_833\" aria-describedby=\"caption-attachment-833\" style=\"width: 725px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-833 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/04part4.png\" alt=\"Figure 4: Apache mod_rewrite example: Using a regular expression match to rewrite an URL\" width=\"725\" height=\"268\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/04part4.png 725w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/04part4-300x111.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><figcaption id=\"caption-attachment-833\" class=\"wp-caption-text\">Figure 4: Using a regular expression match to rewrite an URL<\/figcaption><\/figure>\n<p>If you try any of the above rewrite rules from a machine other than 192.168.0.104, you will see they don\u2019t work as the rewrite rules are only put into effect when the remote address is 192.168.0.104, as you can see in Fig. 5:<\/p>\n<figure id=\"attachment_834\" aria-describedby=\"caption-attachment-834\" style=\"width: 554px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-834 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/05part4.png\" alt=\"Figure 5: Apache mod_rewrite example: Verifying that the rules are valid as per the corresponding condition\" width=\"554\" height=\"290\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/05part4.png 554w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/05part4-300x157.png 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><figcaption id=\"caption-attachment-834\" class=\"wp-caption-text\">Figure 5: Verifying that the rules are valid as per the corresponding condition<\/figcaption><\/figure>\n<p>With a slight change in the RewriteCond directive, you could allow access from the 192.168.0.0\/24 network. Replace<\/p>\n<pre class=\"brush:bash\">RewriteCond \"%{REMOTE_ADDR}\" \"^192\\.168\\.0\\.104\"<\/pre>\n<p>with<\/p>\n<pre class=\"brush:bash\">RewriteCond \"%{REMOTE_ADDR}\" \"^192\\.168\\.0\"<\/pre>\n<p>Then test again (see Fig. 6):<\/p>\n<figure id=\"attachment_835\" aria-describedby=\"caption-attachment-835\" style=\"width: 625px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-835 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/06part4.png\" alt=\"Figure 6: Apache mod_rewrite example: Activating the rules for a given network\" width=\"625\" height=\"334\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/06part4.png 625w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/06part4-300x160.png 300w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/06part4-620x330.png 620w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><figcaption id=\"caption-attachment-835\" class=\"wp-caption-text\">Figure 6: Activating the rules for a given network<\/figcaption><\/figure>\n<p>Please note that you should consider creating custom 404 error pages to display when the visitor attempts to access a resource that does not exist.<\/p>\n<p>For example, copy the following code in \/var\/www\/example1.com\/public_html\/error.html:<\/p>\n<pre class=\"brush:xml\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Not found&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;Not found :(&lt;\/h1&gt;\r\n&lt;h3&gt;The page you requested has not been found.&lt;\/h3&gt;\r\n&lt;p&gt;Perhaps you would like to go to our &lt;a href=\"index.html\"&gt;home page&lt;\/a&gt;?&lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now add the following line inside the virtual host definition:<\/p>\n<pre class=\"brush:bash\">ErrorDocument 404 \/error.html<\/pre>\n<p>Then browse to a non-existent resource (<em>http:\/\/example1.com\/hello<\/em>, for example) and you will see your personalized error page. See Fig. 7 for details:<\/p>\n<figure id=\"attachment_836\" aria-describedby=\"caption-attachment-836\" style=\"width: 430px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-836 size-full\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/07part4.png\" alt=\"Figure 7: Apache mod_rewrite example: A custom error page for non-existing resources\" width=\"430\" height=\"228\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/07part4.png 430w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/02\/07part4-300x160.png 300w\" sizes=\"(max-width: 430px) 100vw, 430px\" \/><figcaption id=\"caption-attachment-836\" class=\"wp-caption-text\">Figure 7: A custom error page for non-existing resources<\/figcaption><\/figure>\n<p>As you can see, a custom error page looks much better than Apache\u2019s default. In addition, you can use the error page to provide instructions (such as the suggestion to go to the home page in the Fig. 7).<\/p>\n<h2>4. Summary<\/h2>\n<p>In this article we have explained how to use mod_rewrite, definitely one of Apache\u2019s most versatile modules, to perform URL rewriting and redirecting. As it is a vast topic, we cannot adequately cover it in a single article, so you are highly encouraged to check out the documentation linked in this tutorial, along with the<a href=\"https:\/\/httpd.apache.org\/docs\/2.4\/rewrite\/remapping.html\" target=\"_blank\" rel=\"noopener\"> Redirecting and Remapping<\/a> guide. This last resource provides lots of other examples of what you can do with mod_rewrite.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we feature a comprehensive Apache mod_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 &hellip;<\/p>\n","protected":false},"author":15,"featured_media":184,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\" \/>\n<meta property=\"og:site_name\" content=\"System Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/systemcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/gacanepa\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-29T15:11:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-31T12:33:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gabriel Canepa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@gacanepa\" \/>\n<meta name=\"twitter:site\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gabriel Canepa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\"},\"author\":{\"name\":\"Gabriel Canepa\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f\"},\"headline\":\"Apache mod_rewrite example: Redirecting and rewriting URLs\",\"datePublished\":\"2016-02-29T15:11:21+00:00\",\"dateModified\":\"2018-05-31T12:33:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\"},\"wordCount\":1724,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg\",\"articleSection\":[\"Apache\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\",\"name\":\"Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg\",\"datePublished\":\"2016-02-29T15:11:21+00:00\",\"dateModified\":\"2018-05-31T12:33:26+00:00\",\"description\":\"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.systemcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Servers\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/web-servers\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Apache\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/web-servers\/apache\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Apache mod_rewrite example: Redirecting and rewriting URLs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"name\":\"System Code Geeks\",\"description\":\"Operating System Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/systemcodegeeks\",\"https:\/\/x.com\/systemcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f\",\"name\":\"Gabriel Canepa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g\",\"caption\":\"Gabriel Canepa\"},\"description\":\"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.\",\"sameAs\":[\"http:\/\/www.gabrielcanepa.com.ar\/\",\"https:\/\/www.facebook.com\/gacanepa\",\"https:\/\/ar.linkedin.com\/in\/gacanepa\",\"https:\/\/x.com\/gacanepa\"],\"url\":\"https:\/\/www.systemcodegeeks.com\/author\/gabriel-canepa\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026","description":"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/","og_locale":"en_US","og_type":"article","og_title":"Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026","og_description":"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.","og_url":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/","og_site_name":"System Code Geeks","article_publisher":"https:\/\/www.facebook.com\/systemcodegeeks","article_author":"https:\/\/www.facebook.com\/gacanepa","article_published_time":"2016-02-29T15:11:21+00:00","article_modified_time":"2018-05-31T12:33:26+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg","type":"image\/jpeg"}],"author":"Gabriel Canepa","twitter_card":"summary_large_image","twitter_creator":"@gacanepa","twitter_site":"@systemcodegeeks","twitter_misc":{"Written by":"Gabriel Canepa","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#article","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/"},"author":{"name":"Gabriel Canepa","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f"},"headline":"Apache mod_rewrite example: Redirecting and rewriting URLs","datePublished":"2016-02-29T15:11:21+00:00","dateModified":"2018-05-31T12:33:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/"},"wordCount":1724,"commentCount":1,"publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg","articleSection":["Apache"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/","url":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/","name":"Apache mod_rewrite example: Redirecting and rewriting URLs - System Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg","datePublished":"2016-02-29T15:11:21+00:00","dateModified":"2018-05-31T12:33:26+00:00","description":"Interested to learn more about apache-mod_rewrite?Then check out our detailed Apache mod_rewrite example where we will explain how to use mod_rewrite to dynamically map incoming HTTP requests targeting arbitrary URLs to specific documents in your web server\u2019s.","breadcrumb":{"@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#primaryimage","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/apache-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.systemcodegeeks.com\/web-servers\/apache\/apache-mod_rewrite-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.systemcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Servers","item":"https:\/\/www.systemcodegeeks.com\/category\/web-servers\/"},{"@type":"ListItem","position":3,"name":"Apache","item":"https:\/\/www.systemcodegeeks.com\/category\/web-servers\/apache\/"},{"@type":"ListItem","position":4,"name":"Apache mod_rewrite example: Redirecting and rewriting URLs"}]},{"@type":"WebSite","@id":"https:\/\/www.systemcodegeeks.com\/#website","url":"https:\/\/www.systemcodegeeks.com\/","name":"System Code Geeks","description":"Operating System Developers Resource Center","publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.systemcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.systemcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/systemcodegeeks","https:\/\/x.com\/systemcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f","name":"Gabriel Canepa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g","caption":"Gabriel Canepa"},"description":"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.","sameAs":["http:\/\/www.gabrielcanepa.com.ar\/","https:\/\/www.facebook.com\/gacanepa","https:\/\/ar.linkedin.com\/in\/gacanepa","https:\/\/x.com\/gacanepa"],"url":"https:\/\/www.systemcodegeeks.com\/author\/gabriel-canepa\/"}]}},"_links":{"self":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/comments?post=827"}],"version-history":[{"count":0,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media\/184"}],"wp:attachment":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media?parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/categories?post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/tags?post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}