Examples of using mod_rewrite for redirects and cnc customization. Forcing HTTPS behind a proxy. Deny a specific file

Mod_rewrite basics and features, mod_rewrite examples, server variables, flags ( RewriteRule Flags), redirection, denial of access by time of day or user agent, denial of access by referrer or in its absence.

Peace be with you, Brothers and Sisters! At the request of the workers, today I'm here like a training program, which is called Oleg, and today we will try to explain, to ourselves, first of all, the basic principles of the miracle module mod_rewrite in order to have a clear understanding of how the conditions and rules work, and not just stupid copy / paste them. So, let's begin...

Apache's mod_rewrite module is a very powerful yet sophisticated tool for manipulating URL redirects / transforms / denials. Virtually any server side URL conversion / manipulation can be done with this miracle module.

Note that some of the URL manipulation does not require a module as powerful and complex (especially for beginners) like mod_rewrite. For simple tasks, mod_alias can be used. By the word "powerful" we mean not only the wide possibilities for converting URLs, but also the increased consumption of server resources in comparison with other modules.

Mod_rewrite Regular Expressions

mod_rewrite uses Perl Compatible Regular Expression ( PCRE - Perl compatible regular expressions ). In this article, we will not describe in detail the use of regular expressions; IMHO, whole volumes of books are devoted to this topic, and one article cannot cover this topic.

  • Secrets of regular expressions: Part 1. Dialects and features. Writing Regular Expressions

The main thing to remember is that regular expressions use Special symbols (metacharacters) and regular characters ( literals). The main metacharacters are \ / ^ $. | ? * + () (). Metacharacters must always be escaped with a backslash "\" - this refers to a space ("\") as well as the same backslash ("\\").

Remember that mod_rewrite uses the original PCRE ( Perl Compatible Regular Expressions), but with some additions:

  • "! Condition" (does not match the condition)
  • "<Условие " (лексически меньше условия)
  • "> Condition" (lexically more than a condition)
  • "= Condition" (lexically equal to condition)
  • "-d" (is a directory)
  • "-f" (is it a regular file)
  • "-s" (whether it is a regular file with a non-zero size)
  • "-l" (is it a symbolic link)
  • "-F" (check for file existence via subquery)
  • "-U" (check if a URL exists via a subquery)

How mod_rewrite rules are processed

The order in which mod_rewrite rules are processed is far from obvious. The mod_rewrite rules are structured in roughly the following order:

RewriteEngine on RewriteBase / # uncomment this line if web-base dir not root # RewriteBase / you-web-base-dir RewriteCond% (what_ to compare) with_ what to compare [flags] RewriteRule source_url target_url [flags]

Now in a little more detail:

  • RewriteEngine - there should be one;
  • RewriteBase - can be useful when using relative links in the rules, but if the relative links refer to the root of the directory, then this directive may not be used, theoretically it can be used repeatedly before each of the rules;
  • RewriteCond - a condition that must be met before the rule is executed, there may be several conditions;
  • RewriteRule is the rule itself, which is executed when the condition is met.

The order in which the .htaccess rules are placed is important because the rewriting engine handles them in a special order. Line by line, the RewriteRule directives are scanned first and if the URL pattern matches ( Pattern, source_url) of a specific rule, the conditions ( RewriteCond directives) related to this rule. Conditions ( RewriteCond) must always come before the rules ( RewriteRule)! In fig. The following shows how mod_rewrite rules are processed.

As you can see, the Current URL is first compared with the Rule Pattern and, if it matches the pattern, it checks the conditions, if the Current URL matches the conditions, then the rule and the Transform are applied to it. The URL goes further for processing if the [L] flag is not specified ( last).

The [L] flag must be used for every rule, unless of course further URL transformation is required.

Mod_rewrite Variables

Server variables can be used in conditions (RewriteCond) and in rules (RewriteRule).

1. HTTP headers (RqH - Request Header):

  • HTTP_USER_AGENT - contains full line header "User-Agent:";
  • HTTP_REFERER - the address from which the user came;
  • HTTP_COOKIE - access to the browser's COOKIE list;
  • HTTP_FORWARDED Contains the IP address of the proxy or load balancer server.
  • HTTP_HOST is the host / server address that the user requested, for example, example.com;
  • HTTP_PROXY_CONNECTION - contains the connection token ( connection-token) "close" or "Keep-Alive", intended for negotiating persistent connections between the client and the server (analogous to the Connection header);
  • HTTP_ACCEPT - Content negotiation header that supports the user's browser / client, for example "text / html, application / xhtml + xml, application / xml; q = 0.9, * / *; q = 0.8" ("type / subtype", separated by commas where type is the content type and subtype is the type qualifier.). If the Accept header contains "Accept: * / *" (* / *), then the client is ready to accept any type of content.

2.connection & request:

  • REMOTE_ADDR - client IP address;
  • REMOTE_HOST - DNS name of the client's IP address;
  • REMOTE_PORT - current client port number;
  • REMOTE_USER - contains the name of the user authorized (by means of the server);
  • REMOTE_IDENT - the variable will be set only if the mod_ident module is connected and IdentityCheck is set to "on", it is designed to work with the Ident protocol https://ru.wikipedia.org/wiki/Ident;
  • REQUEST_METHOD - the method by which the request was made, GET | GET | HEAD, etc .;
  • SCRIPT_FILENAME - full path to the requested file, for example /var/www/public_html/script_name.php;
  • PATH_INFO - Contains the user-supplied path that appears after the script name but before the query string (?). For example, if the script was requested at the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then the $ _SERVER ["PATH_INFO"] variable will contain / some / stuff.
  • QUERY_STRING - a GET request string, if the requested address is http://example.com/index.php?var=1&var=2, then QUERY_STRING will contain var = 1 & var = 2; AUTH_TYPE - authentication type, if HTTP authentication is performed, it can be Basic or Digest.

3.server internals:

  • DOCUMENT_ROOT - full path to the user's home directory, for example / var / www / public_html /
  • SERVER_ADMIN - server / virtual_host administrator data, usually an email address;
  • SERVER_NAME - server name, usually from the ServerName directive;
  • SERVER_ADDR - server IP address;
  • SERVER_PORT - server port;
  • SERVER_PROTOCOL The version of the protocol used, for example HTTP / 1.0 or HTTP / 1.1;
  • SERVER_SOFTWARE - server name / version.

4.date and time (system, date and time):

  • TIME_YEAR - year, 2014
  • TIME_MON - month, 05
  • TIME_DAY - day, 07
  • TIME_HOUR - hour, 04 (24)
  • TIME_MIN - minutes, 38
  • TIME_SEC - seconds, 55
  • TIME_WDAY - day of the week, 3 (Wednesday)
  • TIME - in the format year-month-day-hour-min-sec, for example 20140514234534

5.specials:

  • API_VERSION - in the format "20051115: 33"
  • THE_REQUEST - details of the GET / POST request, for example "GET /index.html HTTP / 1.1"
  • REQUEST_URI - relative url of the request "/index.html"
  • REQUEST_FILENAME - full local path to a file or script in file system corresponding request
  • IS_SUBREQ - if a subquery is executed, then the variable contains true, otherwise false
  • HTTPS - on / off if HTTPS is used / not used

6.variables of the execution result:

  • $ 1 - $ 1, $ 2, etc. generated when (pattern1. *) (pattern2. *) match from RewriteRule
  • % 1 -% 1,% 2, etc. generated when (pattern1. *) (pattern2. *) match from RewriteCond
  • You can read more about HTTP headers in the rfc2616 Hypertext Transfer Protocol - HTTP / 1.1 specification

Mod_rewrite flags

To control the behavior of conditions ( RewriteCond) and rules ( RewriteRule) mod_rewrite uses flags [flags].

  • [B] ( escape backreferences) - forces you to escape (encode) special characters, for example, take the rule "RewriteRule ^ search /(.*)$ /search.php?term=$1" which contains a search string that may contain, for example, "x & y / z "and as a result the string" search.php? term = x & y / z "will be returned, which is not a valid URL and will be converted by the browser to" search.php? term = x% 20 & y% 2Fz = ". With the [B] flag, the string will be converted to "/search.php?term=x%20%26%20y%2Fz". For this example to work, you need to set AllowEncodedSlashes to On because httpd by default does not allow you to encode slashes in URL
  • [C] chain- combine several rules into a chain. If the first rule of the chain does not meet the conditions, then the whole chain will be ignored.
  • cookie- sets a cookie in the format, the parameters for secure and httponly are set to true | false
  • discardpathinfo- discards the PATH_INFO in the converted link, useful in cases where PATH_INFO was already added in the previous conversion
  • [E] env- set a variable or remove it
  • [F] forbidden- returns error 403
  • [G] gone- returns error 410
  • [H] handler- forcibly installs a handler for certain file types, for example, the "RewriteRule! \. -" rule will force all files without an extension to be passed through PHP
  • [L] last- indicates that the rule is the last one and the further transformation process is terminated
  • [N] next- starts the conversion process from the first rule in order, use this flag with caution because it can lead to a closed loop (so-called loop)
  • nocase- disables case checking
  • noescape- mod_rewrite generally applies URI escaping rules to the conversion result. Special characters (such as "%", "$", ";", and so on) will be escaped with their hexadecimal ( hexcode) substitutions ("% 25", "% 24", and "% 3B", respectively). This flag prohibits doing this.
  • nosubreq- ignore subqueries, execute the rule only for real / direct queries
  • [P] proxy- Apache makes a sub-request to the specified page using the mod_proxy program module, and the client will not know anything about this sub-request. An error will occur if the mod_proxy module is not connected
  • passthrough- stop conversion and pass the received new link further
  • qsappend- adds original query parameters ( query string) to be replaced. If no new query parameters are included in the lookup, the original query parameters will be added automatically. If the new parameters are included in the substitution, then the original query parameters will be lost if the QSA flag is not specified
  • [R] redirect- returns the command to redirect to the browser ( the default code is 302 - MOVED TEMPORARY), you can specify the redirect code yourself, for example R = 301 ( code 301 - MOVED PERMANENTLY), but within the range of 300-399, otherwise the rule will not be processed
  • [S] skip- skips the next rule if this rule was triggered. You can specify the number of rules, for example: S = 2
  • [T] type- forcibly sets the MIME type of the target file. For example, "RewriteRule \ .pl $ -", this rule will map Perl scripts to text format, which means the script code will be issued to the browser.

Read more about flags in the original:

Protocols allowed in mod_rewrite

mod_rewrite will define wildcard url as external if one of the following protocols is specified:

  • ajp: // - Apache JServ Protocol
  • balancer: // - Apache Load Balancer
  • ftp: // - File Transfer Protocol
  • gopher: // - Gopher (protocol)
  • http: // - Hypertext Transfer Protocol
  • https: // - Hypertext Transfer Protocol Secure
  • ldap: // - Lightweight Directory Access Protocol
  • nntp: // - Network News Transfer Protocol
  • ldap: - Lightweight Directory Access Protocol
  • mailto: - The mailto URI scheme
  • news: - News Protocol

.htaccess and order of placement of rules

In the .htaccess file, theoretically, directives can be specified at random, with the exception of directives related to rewrite_module. Nevertheless, it is better to follow a certain sequence:

  1. CORE directives;
  2. Configuration of modules.

The rewrite_module directives that perform the redirection, and not the conversion, must be the first, otherwise, after the conversion of the request, the desired redirection may not occur if we do not take into account the previous conversions.

Examples of mod_rewrite rules

Denying access with mod_rewrite

RewriteCond% (TIME_HOUR)% (TIME_MIN)> 2000 RewriteCond% (TIME_HOUR)% (TIME_MIN)<0700 RewriteRule .* - [ F ] # OR RewriteCond %{TIME_HOUR} >= 20 RewriteCond% (TIME_HOUR)<07 RewriteRule .* - [ F ]

This rule will close access from 8 pm to 7 am. The above example specifically for copy / paste lovers contains a deliberate syntax error, HTTP 500 ( server error), which will cause an entry in the error log RewriteRule: bad flag delimiters. It translates as a bad flag separator - instead of [F] you need to use [F], ie avoid spaces and other separators except comma!

2. We firmly forbid the WBSearchBot bot to touch our site:

RewriteCond% (USER_AGENT) WBSearchBot RewriteRule. * - [F] # Alternatively, instead of error 403 (FORBIDDEN), send error 404 (NOT FOUND) RewriteCond% (USER_AGENT) WBSearchBot RewriteRule. * -

Although, having slightly tweaked the rule, you can t.s. transfer arrows to someone else, so to speak, set the bot on another site, they say ... are you, ofonarel !? Go train, over there, on it ... :)) (from X / F Operation "Y" and other adventures of Shurik):

RewriteRule. * Http://kremlin.ru

WBSearchBot ( Mozilla / 5.0 (compatible; WBSearchBot / 1.1; + http: //www.warebay.com/bot.html)) is a rather aggressive bot and you can safely get rid of it.

# BLOCK POST REQUEST FOR OLD HTTP PROTOCOL ... RewriteCond% (THE_REQUEST) ^ POST RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \. | Www1 \.)? Example.com RewriteCond% (THE_REQUEST) POST (. *) HTTP / (0 \ .9 | 1 \ .0) RewriteCond% (HTTP_USER_AGENT) ^ $ RewriteRule. * -

In the previous example, the response code 303 See Other was not specified by chance - the fact is that if the redirection method ( usually GET) differs from the request method ( eg POST), then if response codes 301-302 are returned, instead of an automatic redirect, a page with a link for manual navigation will be displayed in the browser! In the case of the 303 See Other response, the redirection is performed automatically by the GET method, regardless of the request method.

4. Hotlink protection - we prohibit the display of our images from other sites:

# # HOTLINK PROTECT ... # http://www.htaccesstools.com/hotlink-protection/ RewriteCond% (HTTP_REFERER)! ^ $ RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \. | Www1 \.)? RewriteCond site% (HTTP_REFERER)! ^ Http (s)?: // ( www \.)? google.com RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? yandex.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \ .)? subscribe.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? feedburner.com RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.) ? mail.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? poisk.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? rambler .ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? nigma.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? ask.com RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \.)? Qip.ru RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \.)? Ukr.net RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \.)? Conduit.com RewriteCond% (HTTP_REFERER)! ^ Http (s)?: // (www \.)? Tut.by RewriteCond% (HTTP_REFERER )! ^ http (s)?: // (www \.)? bing.com RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? web alta.ru RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? yahoo.com RewriteCond% (HTTP_REFERER)! ^ http (s)?: // (www \.)? conduit. com RewriteRule \. (jpg | jpeg | png | gif) https://dl.dropboxusercontent.com/u/52572427 / \ images / wrs-hotlink-deny .jpg

  • RewriteRule ^ - RewriteCond% (HTTPS) on RewriteRule ^ - # To redirect all users to access the site WITH the "www." prefix, # (http: //example.com / ... will be redirected to http: //www.example.com / ...)# uncomment the following: RewriteCond% (HTTP_HOST). RewriteCond% (HTTP_HOST)! ^ Www \. RewriteRule ^ http% (ENV: protossl): //www.% (HTTP_HOST)% (REQUEST_URI) # OR # REDIRECT FOR / DOCS Redirect permanent / docs http://docs.example.com

    2. Redirecting our RSS / ATOM feeds to FeedBurner

    # REDIRECT blog RSS / ATOM to FeedBurner ... RewriteCond% (HTTP_USER_AGENT)! ^. * (FeedBurner | FeedValidator) RewriteCond% (QUERY_STRING) ^ option = com_content & view = featured & Itemid = () + & format = feed RewriteRule index.php http://feeds.feedburner.com/remote-shaman- blog? # # REDIRECT forum RSS / ATOM to FeedBurner ... # if HTTP_USER_AGENT not FeedBurner or FeedValidator RewriteCond% (HTTP_USER_AGENT)! ^. * (FeedBurner | FeedValidator) # forum / topics / mode-topics? format = feed # forum / topics / mode-latest? format = feed # forum / topics / posts? format = feed# forum / recent? format = feed RewriteCond% (QUERY_STRING) ^ format = feed $ RewriteRule forum / (* /)? () + http://feeds.feedburner.com/remote-shaman-forum?

    Please note that at the end of each link in the rules ( RewriteRule) there is a "?" if you do not specify the "?" symbol, then in the end the redirect will be to the address http: //feeds.feedburner.com/remote-shaman-blog? option = com_content & view = featured & Itemid = ... and http://feeds.feedburner.com / remote-shaman-forum? format = feed respectively.

    Outcomes

    mod_rewrite is a powerful tool and you can do wonders with it - that's a fact! There are not many examples in this article, but there are a lot of them on the network, but there is practically no material about what is what and why in style as for complete idiots. More examples of using mod_rewrite will probably be added later on this page.

    In this article, I tried to explain as clearly as possible (I hope I succeeded) the basic principles of the miracle module mod_rewrite, in a style like for complete idiots (in a good sense of the word;), IMHO from my own experience I know how difficult it is sometimes to master something with zero and when mana and the principles of this something are described in general vague phrases and deeply technical terms.

    When writing this material, only original mana from the site httpd.apache.org was used (no copy / paste). If I missed something or missed something, then be sure to write about it in the comments.

RewriteRule defines the rules for the rewriting engine

Syntax: RewriteRule Pattern Substitution(example, RewriteRule ^ tags $ /tags.php [L] )

In substitution, you can use, among other things, special flags by adding the following construction:

As the third argument to the RewriteRule directive. Flags are a comma-separated list of flags:

  • "redirect | R [= code]" (causes a redirect)

The prefix in the Substitution of the form http: // thishost [: thisport] / (which creates a new URL from some URI) triggers an external redirect (redirect). If there is no code in the substitution, the response will be with HTTP status 302 (TEMPORARILY MOVED). If you want to use other response codes in the 300-400 range, just write them as a number or use one of the following symbolic names: temp (default), permanent, seeother. Use this in directives that must convert some virtual URLs to real ones and return them to the client, for example, convert "/ ~" to "/ u /" or always add a slash to / u / user, etc.

Note: When using this flag, make sure the lookup field is a valid URL! If not, you are redirecting to nowhere! And remember that this flag by itself only prefixes the URL with the string http: // thishost [: thisport] / and the conversion process continues. Also, usually you want to stop and do this redirect immediately. To stop the conversion process, you also need to write the "L" flag.

  • "forbidden | F" (makes URL forbidden)

This makes the current URL prohibited, for example, a response with HTTP status 403 (FORBIDDEN) is immediately sent to the client. Use this flag in conjunction with the appropriate RewriteConds to block URLs based on some criteria.

  • "gone | G" (makes the URL "dead")

This flag makes the current URL "dead", that is, an HTTP response with a 410 (GONE) status is immediately sent. Use this flag to mark dead pages that no longer exist.

  • "proxy | P" (calls a proxy)

This flag marks the wildcard part as an internal proxy request and immediately (i.e., the conversion process stops here) passes it through the proxy module. You must ensure that the substitution string is a real URI (for example, typically starting with http: // hostname) that can be proxied by the Apache module. If it is not, you will get an error from the proxy module. Use this flag to achieve a more powerful implementation of the ProxyPass dirketiv that integrates some content on remote servers into the local server namespace.

Note: To use this make sure you have a working proxy module on your Apache server. If you do not know this, check to see if there is a mod_proxy.c line in the "httpd -l" output. If so, these capabilities are available to mod_rewrite. If not, you must first rebuild the "httpd" program with the proxy module enabled.

  • "last | L" (last rule)

Stop the conversion process at this point and do not apply any further conversion rules. This corresponds to the last statement in Perl or the break statement in C. Use this flag to avoid converting the current URL to other conversion rules that follow. For example, use this to convert the root URL from ("/") to a real one, such as "/ e / www /".

  • "next | N" (next round)

Restart the transformation process (starting with the first rule). In this case, the URL is again matched with certain conditions, but not the original URL, but the URL that came out of the last conversion rule. This corresponds to the next statement in Perl or the continue statement from the C language. Use this flag to restart the conversion process, that is, to jump unconditionally to the beginning of the loop.

However, be careful not to loop endlessly!

  • "chain | C" (linking to the following rule)

This flag binds the current rule to the next one (which, in turn, can be associated with the next one, etc.). This has the following effect: if there is a match for the rule, the process continues as usual, that is, the flag has no effect. If the rule does not match the condition, all subsequent related rules are skipped. For example, use this to remove the ".www" part in the configuration rule of the context of a directory running when you allow external redirects (where ".www" should not be!).

  • "type | T = MIME-type" (force MIME type)

Force the MIME type of the target file to be the MIME type. For example, this can be used to mimic the mod_alias ScriptAlias ​​directive which forces all files inside the mapped directory to have a MIME type of "application / x-httpd-cgi".

  • "nosubreq | NS" (used only in case of non-internal subquery)

This flag instructs the rewriter to skip the directive if the current subquery is an internal subquery. For example, internal subrequests in Apache happen when mod_include tries to get information about possible default files for directories (index.xxx). With subqueries, this is not always useful and even sometimes causes a problem in the operation of the entire set of transformation directives. Use this flag to exclude certain rules.

Use the following rule as you see fit: whenever you prefix some URLs and pass them to the CGI script for processing, chances are good that you run into problems (or even unnecessary overhead) when using subqueries. In these cases, use this flag.

  • "nocase | NC" (case insensitive)

This makes the Template case insensitive, that is, there is no difference between "A-Z" and "a-z" when the Template is applied to the current URL.

  • "qsappend | QSA" - Query String Append

This flag tells the rewriting engine to add, rather than replace, a query string from a URL to an existing one in the lookup string. Use this when you want to add additional data to the query string using transform directives.

An example on learnsongs.ru:

RewriteRule ^ tags / ([- A-Za-z0-9 _ ’] +) $ /tags.php?tag=$1

RewriteRule ^ tags / ([- A-Za-z0-9 _ ’] +)? Page = (+) $ /tags.php?tag=$1&page=$2

  • "noescape | NE" (do not escape URI when outputting)

This flag prevents mod_rewrite from applying normal URI escaping rules to the conversion result. Typically, special characters (such as "%", "$", ";", and so on) will be escaped by their hexadecimal substitutions ("% 25", "% 24", and "% 3B", respectively); this flag prevents you from doing this. This allows percent characters to appear in the output, as in

RewriteRule /foo/(.*) / bar? Arg = P1 \% 3d $ 1

For which "/ foo / zed" would be converted to the safe request "/ bar? Arg = P1 = zed".

  • "passthrough | PT" (pass through the next handler)

This flag instructs the transformation engine to set the uri field of the internal request_rec structure to the filename field. This flag is just a clever trick to be able to handle the output of RewriteRule directives, Alias ​​directives, ScriptAlias, Redirect, etc. from other translators URI file name. A trivial example to show this semantics: if you want to convert / abc to / def using mod_rewrite's conversion engine and then / def to / ghi using mod_alias:

RewriteRule ^ / abc (. *) / Def $ 1

Alias ​​/ def / ghi

If you omit the PT flag, mod_rewrite does the job just fine, i.e. it converts uri = / abc / ... to filename = / def / ... as a fully API-compatible URI-filename translator should do. Then it is the turn of mod_alias to try to make the URI-filename transition which will not work.

Note: You must use this flag if you want to mix directives from different modules containing translators to URL-filename. A typical example is using the mod_alias and mod_rewrite modules ..


Examples of entries in htaccess: Index file, Redirect with rating preservation pages, gluing www and http, Creation CNC or to Human UnderstandUrls, Redirect all files in a folder to one file, Protection against hotlinkov, Definition encodings and much more!

We indicate the index file (which is the first to open when accessing the site)

DirectoryIndex index.php index.html index.shtml
One or more files can be specified

Htaccess php redirect

Redirect / http://www.newsite.ru/
Global redirect (first /) we send EVERYTHING to the new address http://www.newsite.ru/

Redirect / katalog http://www.newsite.ru/newkatalog
All calls to katalog redirect to the domain newsite.ru to section newkatalog

Redirect with preserving page rank

Redirect 301 /old/old.php http://www.yoursite.ru/new.php
old is an example of an old page path. new- an example of a new page path.

Redirect a user with a specific ip

SetEnvIf REMOTE_ADDR 192.100.220.1 REDIR = "redir"
RewriteCond% (REDIR) redir
RewriteRule ^ / $ /out.html
A specific user (he is tired or needs to be sent to the internal section) with the given ip is forwarded to the page /out.html

Pasting www and http htaccess php

Already many SEO (promotion and optimization) know that for Search Engines URLs with / without www- these are different addresses and the availability of a site with www negatively affects indexing and ranking. Therefore, SEO often asks developers to glue the site without www(so that when accessing the site from www, there is a transfer to without www, i.e. only http)

RewriteEngine On
RewriteCond% (HTTP_HOST) ^ www.yoursite \ .ru $
RewriteRule ^ (. *) $ Http://yoursite.ru/$1
Now even if you type in the address bar www.yoursite.ru, then the server will send you to http://yoursite.ru

Creating a CNC or Human-Readable-Urls htaccess php

RewriteEngine on
RewriteRule ^ article /( [^/\.†+)/?$ article.php? Id = $ 1 [L]
After adding this line to .htaccess there will be two addresses available for the document. For example: and www.yoursite.ru/article/1

RewriteEngine on
RewriteRule cat /(.*)/(.*)/$ /art.php?$1=$2
As a result www.yoursite.ru/art.php?type=123 turns into www.yoursite.ru/cat/type/123/:

Here are some more private options:

RewriteEngine on
RewriteRule katalog-saitov [/] * $ article.php? Id = $ 1 [L]
Article with a technical address www.yoursite.ru/article.php?id=1 will now be available with its own human-readable name www.yoursite.ru/katalog-saitov.

Redirect all files in a folder to one file

For example, you no longer need the articles section of your site and want to redirect all requests to the / articles folder to a single /non-articles.php file. The code below will help

RewriteRule ^ articles (. *) $ /Non-articles.php

Htaccess php hotlink protection

Perhaps you are familiar with this option, when images are not uploaded to the server, but simply a link to the site is used. As a result, an extra load of traffic is created for the site that owns the image. We use the code below

RewriteEngine On
# In the line with? Yoursite \ .ru / change this construction to the URL of your site
RewriteCond% (HTTP_REFERER)! ^ Http: // (. + \.)? Yoursite \ .ru /
RewriteCond% (HTTP_REFERER)! ^ $
# Change /images/exit.jpg to a different image. Can be indecent
RewriteRule. * \. (Jpe? G | gif | bmp | png) $ /images/exit.jpg [L]

Htaccess php encoding detection

Additional options for the most popular encodings: UTF-8, Windows-1251, KOI8-R... Let's look at the most common UTF-8 in examples.
AddDefaultCharset UTF8 # file encoding, in which documents are sent by default
AddCharset UTF8 .html # Example: processes html in the given encoding
AddCharset UTF8 * # Example: processes all files in this encoding

# Processing in the given encoding of a specific file

AddCharset UTF8 .html

CharsetDisable On # Cancel the server's re-encoding of uploaded files
CharsetDefault UTF8 # Encoding transmitted by the Server to the Browser by default
CharsetSourceEnc UTF8 # Forced Transcoding of ALL files uploaded to the server

Creating your own error pages

If you want to see after an incorrect link another page, made, for example, by yourself, then we specify the following code in .htaccess (well, and accordingly we make our pages there):

# server error, invalid request
ErrorDocument 400 /error/badrequest.html

# no entry
ErrorDocument 403 /error/forbid.html

# most common - page not found
ErrorDocument 404 /error/notfound.html

# Internal server error
ErrorDocument 500 /error/serverr.html

Denying access

Most likely, you will come across the fact that some of the files and directories will need to be closed from public access.

We close it from everyone

Deny from all

Close a specific file from everyone


deny from all

Allow access from only one ip

Order deny, allow
deny from all
allow from 192.111.37.125

Deny access from specific ip


order allow, deny
allow from all
deny from 192.111.35.122
deny from 192.111.37.171

Remove file extension from Url (URL)

RewriteEngine on
RewriteCond% (REQUEST_FILENAME)! -D
RewriteCond% (REQUEST_FILENAME) \. Html -f
RewriteRule ^ (. *) $ $ 1.php
# php can be replaced with another extension. For example: html, htm, shtml, asp

We prohibit displaying the contents of the directory if there is no index file

You have most likely seen the text at least once. Index of and a list of files. This happens when the directory no index file(for example index.php), and the system prompts you to select a file for further opening. The downside to this is that a random user can see the list and contents of all the files in the directory.
Options -Indexes

For redirecting, configuring CNC on websites using and, and inserting keywords into urls of articles, all directives written in this article are written in the .htaccess file, which is usually located in the root of your site, although in some cms it is in almost every folder but that's a completely different story….

Below are 5 examples of using this module:

1) Modify "product.php? Id = 12" into "product-12.html"

This is a simple redirect in which the .php extension is hidden from the browser address bar and the dynamic URL (with a question mark "?") Is converted to a static address

RewriteEngine on
RewriteRule ^ product - (+) \. Html $ product.php? Id = $ 1

2) Modify "product.php? Id = 12" into "product / ipod-nano / 12.html"

SEO experts always suggest showing the top keyword in the URL. As an example, you can see the name of the product in the URL.

RewriteEngine on
RewriteRule ^ product / (+) / (+) \. Html $ product.php? Id = $ 2

3) Redirecting addresses without www URLs to addresses with www - redirect

If you enter yahoo.com in your browser, you will be redirected to www.yahoo.com. To perform the same operation on your site, add the following code to your .htaccess file:

RewriteEngine On
RewriteCond% (HTTP_HOST) ^ optimaxwebsolutions \ .com $
RewriteRule (. *) Http://www.optimaxwebsolutions.com/$1

The URL of the site, of course, change to your own. Why make such a redirect? To avoid duplication of the site by search engines with and without www.

4) Rewrite "yoursite.com/user.php?username=xyz" into "yoursite.com/xyz"

Add the following lines to the .htaccess file:

RewriteEngine On
RewriteRule ^ (+) $ user.php? Username = $ 1
RewriteRule ^ (+) / $ user.php? Username = $ 1

5) Redirect the domain to a new subdomain or folder.

Let's say you have redesigned your site and the updated site is located in the “new” folder at the root of the site. That is, the new site is available at “test.com/new”. Moving files from one location to another can be quite time consuming, so just add the following lines to your .htaccess file and place it in your root folder:

RewriteEngine On
RewriteCond% (HTTP_HOST) ^ test \ .com $ RewriteCond% (HTTP_HOST) ^ www \ .test \ .com $
RewriteCond% (REQUEST_URI)! ^ / New /
RewriteRule (. *) / New / $ 1

Now when accessing "www.test.com" all files will be taken from "test.com/new"

mod_rewrite: static cnc address with one variable

So, in this article I said that sites on PHP and MySQL have addresses in the following format:

As a rule, such addresses are called dynamic. So now we will deal with the conversion of dynamic addresses to CNC.

Let's say you need to convert from lis.php? id = 3 v bols3.hi... The regular expression will have the following format:

RewriteRule ^ PAGE NAME (+) \. EXTENSION $ PRESENT ADDRESS NAME.php? VARIABLE = $ NUMBER

That is, in our case, we get the following:

RewriteRule ^ bols (+) \. Hi $ lis.php? Id = $ 1

Now instead of the address lis.php? id = 90(where 90 is id) we can safely refer to bols90.hi.

mod_rewrite: static cnc address with many variables

Consider a situation where you need to translate an address with many variables. For example from lis.php? id = 345 & cat = 3 v bols345-3.hi... The situation is similar, but now two GET variables are used. A dash is used as a separator. We get the following expression:

RewriteRule ^ bols (+) - (+) \. Hi $ lis.php? Id = $ 1 & cat = $ 2

Graphically, the transformation principle itself will look like this:

mod_rewrite: archived cnc address

Many archives on the site have an address archive.php? year = 2003 & month = 10... We will transform it into archive / 2003/10 /... We get the following line:

RewriteRule ^ archive / (+) / (+) \ $ archive.php? Year = $ 1 & month = $ 2

mod_rewrite: cnc address for tags

Nowadays, tags are present on almost every blog and website. Let's try to change the address for the winter tag - posts.php? Tag = winter v posts / tags / winter /... We have the following expression:

RewriteRule ^ posts / tags / (+) \ $ posts.php? Tag = $ 1

By the way, search engines use a separate algorithm to index pages with dynamic addresses. I do not know how it differs from the usual, but CNC conversions, again, they help to tell the robot that it needs to index our address, like a regular static page.

andew

2015-02-13T11: 59: 58 + 00: 00

2018-03-02T04: 50: 41 + 00: 00

34141

In this article, I provide a description of the logic of the RewriteRule and the syntax of some directives of the mod_rewrite module of the Apache server. I also highlighted and summarized several conclusions, postulates, which, as it seems to me, must be known and understood when using this module. I hope that all this will allow you, just like me before, to understand the work of this module, which provides powerful functionality for performing various transformations on URLs..

Don't forget in regex shield service symbols. Sometimes they forget to escape a period when it should act exactly as a period, and not as is customary in a regular expression - like any character.

Also, don't forget to wrap the entire rule block for mod_rewrite in the tag:

Don't write too many directives for mod_rewrite, write only those transformation rules that you really need. You especially need to be careful with external redirects - these are redirects that are performed by sending the server header with the code to the client NOT 200 (return of a full-fledged page), and with a different code (most often 301 and 302 ) and which lead to a redirect in the client's browser to another URL i.e. to make a new request on the client. Therefore, any external redirect always leads to a waste of time in processing the request, since you need to send a response to the client, he must read it, and repeat the request using the new URL. This is a time consuming procedure. Therefore, there should be redirects only if you really need them.

Be aware that browsers can cache redirects, while Ctrl + F5 or Ctrl + R does not solve the problem. So disable caching in the browser when testing your rules rewrite module web server Apache.

To find errors in the work of your rules, read the logs. Apache.

Do not make a dull copy-paste of directives from various articles on the Internet into your apache configuration files. Articles are written by people and therefore erroneous writing of directives is possible, and if we take into account that the method of writing articles also involves the active use of copy-paste, then errors can spread and multiply. Use the articles only as a reference, and write the directives from the manuals into your configuration files. Although this also does not guarantee one hundred percent of errors, at least you minimize them.

In this article, I described only two directives and the main concepts in my opinion. However, as you yourself understand, mod_rewrite provides many other directives and functionality.

Did you like the article? To share with friends: