github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_2_34/manual/rewrite/avoid.html.en (about)

     1  <?xml version="1.0" encoding="ISO-8859-1"?>
     2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     3  <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
     4  <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
     5  <!--
     6          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     7                This file is generated from xml source: DO NOT EDIT
     8          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     9        -->
    10  <title>When not to use mod_rewrite - Apache HTTP Server Version 2.2</title>
    11  <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
    12  <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
    13  <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
    14  <script src="../style/scripts/prettify.min.js" type="text/javascript">
    15  </script>
    16  
    17  <link href="../images/favicon.ico" rel="shortcut icon" /><link href="http://httpd.apache.org/docs/current/rewrite/avoid.html" rel="canonical" /></head>
    18  <body id="manual-page"><div id="page-header">
    19  <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
    20  <p class="apache">Apache HTTP Server Version 2.2</p>
    21  <img alt="" src="../images/feather.gif" /></div>
    22  <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
    23  <div id="path">
    24  <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.2</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div class="retired"><h4>Please note</h4>
    25              <p> This document refers to a legacy release (<strong>2.2</strong>) of Apache httpd. The active release (<strong>2.4</strong>) is documented <a href="http://httpd.apache.org/docs/current">here</a>. If you have not already upgraded, please follow <a href="http://httpd.apache.org/docs/current/upgrading.html">this link</a> for more information.</p>
    26          <p>You may follow <a href="http://httpd.apache.org/docs/current/rewrite/avoid.html">this link</a> to go to the current version of this document.</p></div><div id="preamble"><h1>When not to use mod_rewrite</h1>
    27  <div class="toplang">
    28  <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
    29  </div>
    30  
    31  
    32  <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> 
    33  <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
    34  perhaps one of the most important concepts about mod_rewrite - namely,
    35  when to avoid using it.</p>
    36  
    37  <p>mod_rewrite should be considered a last resort, when other
    38  alternatives are found wanting. Using it when there are simpler
    39  alternatives leads to configurations which are confusing, fragile, and
    40  hard to maintain. Understanding what other alternatives are available is
    41  a very important step towards mod_rewrite mastery.</p>
    42  
    43  <p>Note that many of these examples won't work unchanged in your
    44  particular server configuration, so it's important that you understand
    45  them, rather than merely cutting and pasting the examples into your
    46  configuration.</p>
    47  
    48  <p>The most common situation in which <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> is
    49  the right tool is when the very best solution requires access to the
    50  server configuration files, and you don't have that access. Some
    51  configuration directives are only available in the server configuration
    52  file. So if you are in a hosting situation where you only have .htaccess
    53  files to work with, you may need to resort to
    54  <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>.</p>
    55  
    56  </div>
    57  <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#redirect">Simple Redirection</a></li>
    58  <li><img alt="" src="../images/down.gif" /> <a href="#alias">URL Aliasing</a></li>
    59  <li><img alt="" src="../images/down.gif" /> <a href="#vhosts">Virtual Hosting</a></li>
    60  <li><img alt="" src="../images/down.gif" /> <a href="#proxy">Simple Proxying</a></li>
    61  </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="rewritemap.html">Using RewriteMap</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
    62  <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
    63  <div class="section">
    64  <h2><a name="redirect" id="redirect">Simple Redirection</a></h2>
    65  
    66  
    67  <p><code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> provides the <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code> and <code class="directive"><a href="../mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directives, which provide a
    68  means to redirect one URL to another. This kind of simple redirection of
    69  one URL, or a class of URLs, to somewhere else, should be accomplished
    70  using these directives rather than <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. <code>RedirectMatch</code>
    71  allows you to include a regular expression in your redirection criteria,
    72  providing many of the benefits of using <code>RewriteRule</code>.</p>
    73  
    74  <p>A common use for <code>RewriteRule</code> is to redirect an entire
    75  class of URLs. For example, all URLs in the <code>/one</code> directory
    76  must be redirected to <code>http://one.example.com/</code>, or perhaps
    77  all <code>http</code> requests must be redirected to
    78  <code>https</code>.</p>
    79  
    80  <p>These situations are better handled by the <code>Redirect</code>
    81  directive. Remember that <code>Redirect</code> preserves path
    82  information. That is to say, a redirect for a URL <code>/one</code> will
    83  also redirect all URLs under that, such as <code>/one/two.html</code>
    84  and <code>/one/three/four.html</code>.</p>
    85  
    86  <p>To redirect URLs under <code>/one</code> to
    87  <code>http://one.example.com</code>, do the following:</p>
    88  
    89  <div class="example"><p><code>
    90  Redirect /one/ http://one.example.com/
    91  </code></p></div>
    92  
    93  <p>To redirect <code>http</code> URLs to <code>https</code>, do the
    94  following:</p>
    95  
    96  <div class="example"><p><code>
    97  &lt;VirtualHost *:80&gt;
    98  ServerName www.example.com<br />
    99  Redirect / https://www.example.com/<br />
   100  &lt;/VirtualHost &gt;
   101  <br />
   102  &lt;VirtualHost *:443&gt;
   103  ServerName www.example.com<br />
   104  <br />
   105  # ... SSL configuration goes here<br />
   106  &lt;/VirtualHost &gt;
   107  </code></p></div>
   108  
   109  <p>The use of <code>RewriteRule</code> to perform this task may be
   110  appropriate if there are other <code>RewriteRule</code> directives in
   111  the same scope. This is because, when there are <code>Redirect</code>
   112  and <code>RewriteRule</code> directives in the same scope, the
   113  <code>RewriteRule</code> directives will run first, regardless of the
   114  order of appearance in the configuration file.</p>
   115  
   116  <p>In the case of the <em>http-to-https</em> redirection, the use of
   117  <code>RewriteRule</code> would be appropriate if you don't have access
   118  to the main server configuration file, and are obliged to perform this
   119  task in a <code>.htaccess</code> file instead.</p>
   120  
   121  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
   122  <div class="section">
   123  <h2><a name="alias" id="alias">URL Aliasing</a></h2>
   124  <p>The <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> directive
   125  provides mapping from a URI to a directory - usually a directory outside
   126  of your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>. Although it
   127  is possible to perform this mapping with <code>mod_rewrite</code>,
   128  <code>Alias</code> is the preferred method, for reasons of simplicity
   129  and performance.</p>
   130  
   131  <div class="example"><h3>Using Alias</h3><p><code>
   132  Alias /cats /var/www/virtualhosts/felines/htdocs
   133  </code></p></div>
   134  
   135  <p>
   136  The use of <code>mod_rewrite</code> to perform this mapping may be
   137  appropriate when you do not have access to the server configuration
   138  files. Alias may only be used in server or virtualhost context, and not
   139  in a <code>.htaccess</code> file.
   140  </p>
   141  
   142  <p>Symbolic links would be another way to accomplish the same thing, if
   143  you have <code>Options FollowSymLinks</code> enabled on your
   144  server.</p>
   145  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
   146  <div class="section">
   147  <h2><a name="vhosts" id="vhosts">Virtual Hosting</a></h2>
   148  <p>Although it is possible to handle <a href="vhosts.html">virtual hosts
   149  with mod_rewrite</a>, it is seldom the right way. Creating individual
   150  &lt;VirtualHost&gt; blocks is almost always the right way to go. In the
   151  event that you have an enormous number of virtual hosts, consider using
   152  <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> to create these hosts automatically.</p>
   153  
   154  <p>Third-party modules such as <a href="http://people.apache.org/~fabien/mod_macro/">mod_macro</a> are
   155  also useful for creating a large number of virtual hosts dynamically.</p>
   156  
   157  <p>Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> for vitualhost creation may be
   158  appropriate if you are using a hosting service that does not provide
   159  you access to the server configuration files, and you are therefore
   160  restricted to configuration using <code>.htaccess</code> files.</p>
   161  
   162  <p>See the <a href="vhosts.html">virtual hosts with mod_rewrite</a>
   163  document for more details on how you might accomplish this if it still
   164  seems like the right approach.</p>
   165  
   166  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
   167  <div class="section">
   168  <h2><a name="proxy" id="proxy">Simple Proxying</a></h2>
   169  
   170  <p><code>RewriteRule</code> provides the <a href="flags.html#flag_p">[P]</a> flag to pass rewritten URIs through
   171  <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>.</p>
   172  
   173  <div class="example"><p><code>
   174  RewriteRule ^/?images(.*) http://imageserver.local/images$1 [P]
   175  </code></p></div>
   176  
   177  <p>However, in many cases, when there is no actual pattern matching
   178  needed, as in the example shown above, the <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code> directive is a better choice.
   179  The example here could be rendered as:</p>
   180  
   181  <div class="example"><p><code>
   182  ProxyPass /images/ http://imageserver.local/images/
   183  </code></p></div>
   184  
   185  <p>Note that whether you use <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> or <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code>, you'll still need to use the
   186  <code class="directive"><a href="../mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a></code> directive to
   187  catch redirects issued from the back-end server:</p>
   188  
   189  <div class="example"><p><code>
   190  ProxyPassReverse /images/ http://imageserver.local/images/
   191  </code></p></div>
   192  
   193  <p>You may need to use <code>RewriteRule</code> instead when there are
   194  other <code>RewriteRule</code>s in effect in the same scope, as a
   195  <code>RewriteRule</code> will usually take effect before a
   196  <code>ProxyPass</code>, and so may preempt what you're trying to
   197  accomplish.</p>
   198  
   199  </div></div>
   200  <div class="bottomlang">
   201  <p><span>Available Languages: </span><a href="../en/rewrite/avoid.html" title="English">&nbsp;en&nbsp;</a></p>
   202  </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
   203  <script type="text/javascript"><!--//--><![CDATA[//><!--
   204  var comments_shortname = 'httpd';
   205  var comments_identifier = 'http://httpd.apache.org/docs/2.2/rewrite/avoid.html';
   206  (function(w, d) {
   207      if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
   208          d.write('<div id="comments_thread"><\/div>');
   209          var s = d.createElement('script');
   210          s.type = 'text/javascript';
   211          s.async = true;
   212          s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
   213          (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
   214      }
   215      else { 
   216          d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
   217      }
   218  })(window, document);
   219  //--><!]]></script></div><div id="footer">
   220  <p class="apache">Copyright 2017 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
   221  <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
   222  if (typeof(prettyPrint) !== 'undefined') {
   223      prettyPrint();
   224  }
   225  //--><!]]></script>
   226  </body></html>