Apache rewrite rules common application (REWRITE)

xiaoxiao2021-03-06  47

First, the purpose

This article is intended to provide how to use Apache to rewrite rules to solve some common URL rewriting methods, and give users some basic methods and clues to users through a common instance.

Two: Why do you need to rewrite rules? A website, if it is a long-term need to be placed on the Internet, it must be continuously updated and maintained, such as moving to other servers for maintenance, reorganizing the directory structure, transform the URL and even change to new domain name, etc. In order to make customers unaffected, the best way is to use the Apache Rewrite Rule (rewrite rules).

Three: Scope of Rewrite Rules 1) You can use the Apache master configuration file httpd.conf 2) You can use 3 in the virtual host configuration defined in httpd.conf) You can use the across configuration files in the basic directory. Htaccess

Four: Application conditions for rewrite rules Only when the user's web request is finally directed to a web server's Apache background, this web server accepts the request, according to the configuration file, the request is the main configuration or the virtual host, according to the basis The URI requested in the browser is paired to override the rules and paired the rewrite rules in Htaccess according to the actual request path. Finally, pass the request to the user, the response may have two:

1) The external redirection of the browser request content to another URL. Let the browser issued a request in a new URI (R = 301 or R = 302, temporary or permanent redirection), such as: a website has a regular URL and alias URL, redirect the alias URL to the regular URL, Or the website is converted into a new domain name redirects the old domain name to a new domain name (redirect)

2) It is also possible to generate new content from the Apache internal sub-request agent [P, L] This is the request content and send back the content through the agent module within the URI after the rewrite URI, and the client The browser doesn't know that the URI in the browser will not be rewritten. However, the actual content is obtained by Apache's URI after rewriting rules. Such as: Apache running on the company's firewall launches this proxy rewrite rule, the agent requests the web server on the internal network segment.

Five: What is the work of rewriting rules? We assume that MOD_REWRITE has been compiled into a module when compiling Apache, ensuring that there is loadModule Rewrite_Module Libexec / Mod_rewrite.so in your httpd.conf and there is AddModule MOD_REWRITE.C in AddModule, you can use rewrite rules. When an external request comes to Apache, Apache calls the definition in the rewrite rule to rewrite the URI specified by the user browser, and finally rewritten URIs are redirected, then send by the browser again; if it is The agent handed over the reserved URI to the agent module request final content (final), finally sent the content back to the browser.

Six: When is the rewrite rule definition in Htaccess? If you don't have administrator privileges, or your website is hosted on the ISP server, you can't rewrite the main profile, however you can be on your Web site content. The directory has write permissions, you can set your own.htaccess file to achieve the same purpose. But you need to make sure that the directory where your website is located in the main configuration file defines the following content: Options INDEXES FOLLOWSYMLINKS ALLOWOVERRIDE ALL

Otherwise, your .htaccess will not work.

7: Application examples assume that Apache is compiled and installed under the / usr / local / apache / directory of the host 192.168.1.56, we have compiled the rewriting and proxy modules.

1) Hide a directory under Apache so that any requests to the directory are redirected to another file.

A> Implementation method of httpd.conf

We put down the part to /usr/local/apache/conf/httpd.conf

Options INDEXES FOLLOWSYMLINKS Allowoverride All RewriteEngine on shutritebase / REWRITERULE ^ (. *) $ index.html.en [r = 301]

Note: ReWriteEngine ON is a rewriting engine switch, if set to OFF, any rewrite rule definition will not be used, the other advantage of the switch is that if you temporarily take off the rewrite rules, change it to OFF and start Apache, you don't have to comment out the rewrite rules of the following. The REWRITEBASE / role is that if the part after the rewritten (herein "here index.html.en is not /, it is the relative directory, which is the definition of this RewriteBase is also / USR / LOCAL / APACHE / HTDOCS / INDEX.HTML.EN, otherwise, if there is no RewriteBase / this item here, it is rewritten to http://192.168.1.56/usr/local/apache/htdocs/manual/index. Html.en is obviously incorrect.

However, we can also use REWRITEENGINE IN REWRITERULE ^ (. *) $ /Dex.html.en [r = 301] or REWRITEENGINE ON REWRITERULE ^ (. *) $ Http://192.168.1.56/ Index.html.en [r = 301]

B> .htaccess implementation method

Let's put the following part to httpd.conf

Options INDEXES FOLLOWSYMLINKS ALOWOVERRIDE ALL

Then put the bottom part to /usr/local/apache/htdocs/manual/.htaccess RewriteEngine on shutritebase / shutriterule ^ (. *) $ Index.html.en [r = 301]

Note: Any changes made to the file. Htaccess do not need to restart Apache.

Q: If you redirect this manual directory to your own home directory? Use the below .htaccess solution. ReWriteEngine on shutritebase / ~ jephe / turnriterule ^ (. *) $ 1 [r = 301] The request for any file in the Manual directory is redirected to the same file under ~ Jephe directory.

2) Convert WWW.USERNAME.DOMAIN.COM for username's home page request for www.domain.com/username

For the request for HTTP / 1.1 includes a host: http header, we can use the following rule set http://www.username.domain.com/anypath to / home / username / annpath

ReWriteEngine on shutritecond% {http_host} ^ www /.[1) .]/. Host / .com $ RITERULE ^ (. )% {Http_host} $ 1 [c] REWRITERULE ^ WWW /.([1) .host / .com (. *) / home / $ 1 $ 2

Note: RewriteCond condition rewrite rules, before applying the following rewrite rules, RewriteCond has various variables, please refer to the relevant documentation.

3) Rewriting rules on the firewall Agent requests on the internal network segment.

NamevirtualHost 1.2.3.4

Servername www.domain.com RewriteEngine on proxyRequest on shutriterule ^ / (. *) $ http://192.168.1.3/$ [p, l]

Note: When the external browser requests www.domain.com, it is resolved to the IP address 1.2.3.4. Apache hands into mod_rewrite processing to http://192.168.1.3/! User browser.

4) Basic predetermined conversion MAP table to overwrive the REWRITEMAP

Convert www.domain.com/{ ketath to the URI specified in the MAP table, the above is the definition in the virtual host

Rewritelog /usr/local/apache/logs/rewrite.log Rewriteloglevel 9

RewriteEngine on proxyRequest on shutritemap sitem/conf/rewr/.map rEWRITERULE ^ / ([^ /] ) / (. *) $ http: //% {remote_host}: $ 1 [c] Ruignerule (. *) :: ([AZ] ) $ {Sitemap: $ 2 | http:// hijk /} [= 301, l]

The contents of the file /us/local/apache/conf/rewrite.map are as follows:

SG http: //a.b.c.d/ sh http://e.f.g.h/

转载请注明原文地址:https://www.9cbs.com/read-80435.html

New Post(0)