Redirecting and Remapping with mod_rewrite - Apache HTTP Server Version 2.4

Modules | Directives | FAQ | Glossary | Sitemap
Apache HTTP Server Version 2.4

Apache > HTTP Server > Documentation > Version 2.4 > RewriteRedirecting and Remapping with mod_rewrite

Available Languages:  en  |

This document supplements the mod_rewrite
reference documentation. It describes
how you can use mod_rewrite to redirect and remap
request. This includes many examples of common uses of mod_rewrite,
including detailed descriptions of how each works.

Note that many of these examples won't work unchanged in your
particular server configuration, so it's important that you understand
them, rather than merely cutting and pasting the examples into your

 From Old to New (internal)
 Rewriting From Old to New (external)
 Resource Moved to Another Server
 From Static to Dynamic
 Backward Compatibility for file extension change
 Canonical Hostnames
 Search for pages in more than one directory
 Redirecting to Geographically Distributed Servers
 Browser Dependent Content
 Canonical URLs
 Moved DocumentRoot
 Fallback Resource
 Rewrite query string
See alsoModule documentationmod_rewrite introductionControlling accessVirtual hostsProxyingUsing RewriteMapAdvanced techniquesWhen not to use mod_rewriteComments

From Old to New (internal)



      Assume we have recently renamed the page
      foo.html to bar.html and now want
      to provide the old URL for backward compatibility. However,
      we want that users of the old URL even not recognize that
      the pages was renamed - that is, we don't want the address to
      change in their browser.


      We rewrite the old URL to the new one internally via the
      following rule:

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "/bar.html" [PT]


Rewriting From Old to New (external)



      Assume again that we have recently renamed the page
      foo.html to bar.html and now want
      to provide the old URL for backward compatibility. But this
      time we want that the users of the old URL get hinted to
      the new one, i.e. their browsers Location field should
      change, too.


      We force a HTTP redirect to the new URL which leads to a
      change of the browsers and thus the users view:

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "bar.html"  [R]


    In this example, as contrasted to the internal example above, we can simply
    use the Redirect directive. mod_rewrite was used in that earlier
    example in order to hide the redirect from the client:

    Redirect "/foo.html" "/bar.html"


Resource Moved to Another Server



      If a resource has moved to another server, you may wish to have
      URLs continue to work for a time on the old server while people
      update their bookmarks.


      You can use mod_rewrite to redirect these URLs
      to the new server, but you might also consider using the Redirect
      or RedirectMatch directive.

#With mod_rewrite
RewriteEngine on
RewriteRule   "^/docs/(.+)"  "$1"  [R,L]

#With RedirectMatch
RedirectMatch "^/docs/(.*)" "$1"

#With Redirect
Redirect "/docs/" ""


From Static to Dynamic



      How can we transform a static page
      foo.html into a dynamic variant
      foo.cgi in a seamless way, i.e. without notice
      by the browser/user.


      We just rewrite the URL to the CGI-script and force the
      handler to be cgi-script so that it is
      executed as a CGI program.
      This way a request to /~quux/foo.html
      internally leads to the invocation of

RewriteEngine  on
RewriteBase    "/~quux/"
RewriteRule    "^foo\.html$"  "foo.cgi"  [H=cgi-script]


Backward Compatibility for file extension change



      How can we make URLs backward compatible (still
      existing virtually) after migrating document.YYYY
      to document.XXXX, e.g. after translating a
      bunch of .html files to .php?


      We rewrite the name to its basename and test for
      existence of the new extension. If it exists, we take
      that name, else we rewrite the URL to its original state.

#   backward compatibility ruleset for
#   rewriting document.html to document.php
#   when and only when document.php exists
<Directory "/var/www/htdocs">
    RewriteEngine on
    RewriteBase "/var/www/htdocs"

    RewriteCond "$1.php" -f
    RewriteCond "$1.html" !-f
    RewriteRule "^(.*).html$" "$1.php"


    This example uses an often-overlooked feature of mod_rewrite,
    by taking advantage of the order of execution of the ruleset. In
    particular, mod_rewrite evaluates the left-hand-side of the
    RewriteRule before it evaluates the RewriteCond directives.
    Consequently, $1 is already defined by the time the RewriteCond
    directives are evaluated. This allows us to test for the existence
    of the original (document.html) and target
    (document.php) files using the same base filename.

    This ruleset is designed to use in a per-directory context (In a
    <Directory> block or in a .htaccess file), so that the
    -f checks are looking at the correct directory path.
    You may need to set a RewriteBase directive to specify the
    directory base that you're working in.

Canonical Hostnames


        The goal of this rule is to force the use of a particular
        hostname, in preference to other hostnames which may be used to
        reach the same site. For example, if you wish to force the use
        of instead of, you might use a variant of the
        following recipe.



The very best way to solve this doesn't involve mod_rewrite at all,
but rather uses the Redirect
directive placed in a virtual host for the non-canonical

<VirtualHost *:80>

  Redirect "/" ""

<VirtualHost *:80>

You can alternatively accomplish this using the

<If "%{HTTP_HOST} != ''">
    Redirect "/" ""

Or, for example, to redirect a portion of your site to HTTPS, you
might do the following:

    Redirect "/admin/" ""

If, for whatever reason, you still want to use mod_rewrite
- if, for example, you need this to work with a larger set of RewriteRules -
you might use one of the recipes below.

For sites running on a port other than 80:
RewriteCond "%{HTTP_HOST}"   "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteCond "%{SERVER_PORT}" "!^80$"
RewriteRule "^/?(.*)"        "{SERVER_PORT}/$1" [L,R,NE]

And for a site running on port 80
RewriteCond "%{HTTP_HOST}"   "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteRule "^/?(.*)"        "$1" [L,R,NE]

        If you wanted to do this generically for all domain names - that
        is, if you want to redirect to for all possible values of, you could use the following

RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]

    These rulesets will work either in your main server configuration
    file, or in a .htaccess file placed in the DocumentRoot of the server.

Search for pages in more than one directory



      A particular resource might exist in one of several places, and
      we want to look in those places for the resource when it is
      requested. Perhaps we've recently rearranged our directory
      structure, dividing content into several locations.


      The following ruleset searches in two directories to find the
      resource, and, if not finding it in either place, will attempt to
      just serve it out of the location requested.

RewriteEngine on

#   first try to find it in dir1/...
#   ...and if found stop and be happy:
RewriteCond         "%{DOCUMENT_ROOT}/dir1/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir1/$1"  [L]

#   second try to find it in dir2/...
#   ...and if found stop and be happy:
RewriteCond         "%{DOCUMENT_ROOT}/dir2/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/dir2/$1"  [L]

#   else go on for other Alias or ScriptAlias directives,
#   etc.
RewriteRule   "^"  "-"  [PT]


Redirecting to Geographically Distributed Servers



    We have numerous mirrors of our website, and want to redirect
    people to the one that is located in the country where they are


    Looking at the hostname of the requesting client, we determine
    which country they are coming from. If we can't do a lookup on their
    IP address, we fall back to a default server.
    We'll use a RewriteMap
    directive to build a list of servers that we wish to use.

HostnameLookups on
RewriteEngine on
RewriteMap    multiplex         "txt:/path/to/map.mirrors"
RewriteCond   "%{REMOTE_HOST}"  "([a-z]+)$" [NC]
RewriteRule   "^/(.*)$"  "${multiplex:%1|}$1"  [R,L]

##  map.mirrors -- Multiplexing Map



    This ruleset relies on
    being set on, which can be
    a significant performance hit.

    The RewriteCond
    directive captures the last portion of the hostname of the
    requesting client - the country code - and the following RewriteRule
    uses that value to look up the appropriate mirror host in the map

Browser Dependent Content



      We wish to provide different content based on the browser, or
      user-agent, which is requesting the content.


      We have to decide, based on the HTTP header "User-Agent",
      which content to serve. The following config
      does the following: If the HTTP header "User-Agent"
      contains "Mozilla/3", the page foo.html
      is rewritten to foo.NS.html and the
      rewriting stops. If the browser is "Lynx" or "Mozilla" of
      version 1 or 2, the URL becomes foo.20.html.
      All other browsers receive page foo.32.html.
      This is done with the following ruleset:

RewriteCond "%{HTTP_USER_AGENT}"  "^Mozilla/3.*"
RewriteRule "^foo\.html$"         "foo.NS.html"          [L]

RewriteCond "%{HTTP_USER_AGENT}"  "^Lynx/" [OR]
RewriteCond "%{HTTP_USER_AGENT}"  "^Mozilla/[12]"
RewriteRule "^foo\.html$"         "foo.20.html"          [L]

RewriteRule "^foo\.html$"         "foo.32.html"          [L]


Canonical URLs


     On some webservers there is more than one URL for a
     resource. Usually there are canonical URLs (which are be
     actually used and distributed) and those which are just
     shortcuts, internal ones, and so on. Independent of which URL the
     user supplied with the request, they should finally see the
     canonical one in their browser address bar.


       We do an external HTTP redirect for all non-canonical
       URLs to fix them in the location view of the Browser and
       for all subsequent requests. In the example ruleset below
       we replace /puppies and /canines
       by the canonical /dogs.

RewriteRule   "^/(puppies|canines)/(.*)"    "/dogs/$2"  [R]


     This should really be accomplished with Redirect or RedirectMatch

     RedirectMatch "^/(puppies|canines)/(.*)" "/dogs/$2"


Moved DocumentRoot



Usually the DocumentRoot
of the webserver directly relates to the URL "/".
But often this data is not really of top-level priority. For example,
you may wish for visitors, on first entering a site, to go to a
particular subdirectory /about/. This may be accomplished
using the following ruleset:


      We redirect the URL / to

RewriteEngine on
RewriteRule   "^/$"  "/about/"  [R]

Note that this can also be handled using the RedirectMatch directive:

RedirectMatch "^/$" ""

Note also that the example rewrites only the root URL. That is, it
rewrites a request for, but not a
request for If you have in
fact changed your document root - that is, if all of
your content is in fact in that subdirectory, it is greatly preferable
to simply change your DocumentRoot
directive, or move all of the content up one directory,
rather than rewriting URLs.

Fallback Resource

You want a single resource (say, a certain file, like index.php) to
handle all requests that come to a particular directory, except those
that should go to an existing resource such as an image, or a css file.


As of version 2.2.16, you should use the FallbackResource directive for this:

<Directory "/var/www/my_blog">
  FallbackResource "index.php"

However, in earlier versions of Apache, or if your needs are more
complicated than this, you can use a variation of the following rewrite
set to accomplish the same thing:

<Directory "/var/www/my_blog">
  RewriteBase "/my_blog"

  RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-f
  RewriteCond "/var/www/my_blog/%{REQUEST_FILENAME}" !-d
  RewriteRule "^" "index.php" [PT]

If, on the other hand, you wish to pass the requested URI as a query
string argument to index.php, you can replace that RewriteRule with:

RewriteRule "(.*)" "index.php?$1" [PT,QSA]

Note that these rulesets can be used in a .htaccess
file, as well as in a <Directory> block.

Rewrite query string

You want to capture a particular value from a query string
and either replace it or incorporate it into another component
of the URL.


 Many of the solutions in this section will all use the same condition,
which leaves the matched value in the %2 backreference.  %1 is the beginining
of the query string (up to the key of intererest), and %3 is the remainder. This
condition is a bit complex for flexibility and to avoid double '&&' in the

  This solution removes the matching key and value:

# Remove mykey=???
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteRule "(.*)" "$1?%1%3"


  This solution uses the captured value in the URL subsitution,
  discarding the rest of the original query by appending a '?':

# Copy from query string to PATH_INFO
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteRule "(.*)" "$1/products/%2/?" [PT]


  This solution checks the captured value in a subsequent condition:

# Capture the value of mykey in the query string
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))mykey=([^&]*)&?(.*)&?$"
RewriteCond "%2" !=not-so-secret-value 
RewriteRule "(.*)" - [F]


  This solution shows the reverse of the previous ones, copying
      path components (perhaps PATH_INFO) from the URL into the query string.
# The desired URL might be /products/kitchen-sink, and the script expects
# /path?products=kitchen-sink.
RewriteRule "^/?path/([^/]+)/([^/]+)" "/path?$1=$2" [PT]


Available Languages:  en  |
CommentsNotice:This is not a Q&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 mailing lists.

Copyright 2017 The Apache Software Foundation.Licensed under the Apache License, Version 2.0.
Modules | Directives | FAQ | Glossary | Sitemap
page_1 | page_2 | page_3 | page_4 | page_5 | сальса.рф
Warning: simplexml_load_file(): sites/ parser error : Extra content at the end of the document in /home/artem/pool/index.php on line 77

Warning: simplexml_load_file(): t> in /home/artem/pool/index.php on line 77

Warning: simplexml_load_file(): ^ in /home/artem/pool/index.php on line 77

Fatal error: Call to a member function xpath() on a non-object in /home/artem/pool/index.php on line 82