Apache Rewrite implements 301 jumps and domain jumps for URL

  • 2020-05-06 12:05:19
  • OfStack

If you want to use the rewrite module, you must first install or load the rewrite module. There are two methods: one is to directly install rewrite module when compiling apache, and the other is to install apache module in DSO mode when compiling apache, and then use the source code and apxs to install rewrite module.

There are two methods for server-based (httpd.conf). One is to directly use RewriteEngine on to open rewrite function in the global context of httpd.conf. The other is to use RewriteEngine on locally to turn on rewrite. As an example, it is important to note that rewrite must be turned on with RewriteEngine on in each virtualhost. Otherwise virtualhost will not have RewriteEngine on in it and its rules will not be in effect.

Based on the directory level (.htaccess), it is important to note that you must open the FollowSymLinks property of this directory and declare RewriteEngine on in.htaccess.

2. Example:

The following are the rules defined in a virtual host. The function is to redirect the client request host prefix is not www.jb51.net and 70.40.213.183 to the host prefix is // www.jb51.net, avoid the same content of the web page has multiple points to the domain name, such as http:// jb51.net.
 
NameVirtualHost 70.40.213.183:80 
ServerAdmin slj@jb51.net 
DocumentRoot  " /web "  
ServerName jb51.net 

RewriteEngine on # Open the rewirte function  
RewriteCond %{HTTP_HOST} !^www.jb51.net [NC] # The statement Client The prefix in the requested host is not www.jb51.net , including  [NC]  Ignore case  
RewriteCond %{HTTP_HOST} !^70.40.213.183 [NC] # The statement Client The prefix in the requested host is not 70.40.213.183 , including  [NC]  Ignore case  
RewriteCond %{HTTP_HOST} !^$ # The statement Client The prefix in the requested host is not null  
RewriteRule ^(.*) //www.jb51.net/ [L] # Meaning if Client If the prefix in the requested host meets the above criteria, jump to //www.jb51.net/,[L] Means that the rewrite operation is stopped immediately and no other rewrite rules are applied. Here, .* It means match everything URL Contains no newline characters, () The function of the parenthesis is to make a mark for all characters for later application . That's the quote from the previous one  (.*) Characters.  

Example 2. Jump to www.jb51.net
when entering the domain name en.jb51.net
 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^en.jb51.net [NC] 
RewriteRule ^(.*) //www.jb51.net/ [L] 

The new domain name is www.jb51.net, which is shorter and easier to remember. Then you need to the original domain name ss. jb51. net, and BBS host address ss. jb51. net bbs/directed to the new domain name, so that users can find, and make the original BBS URL continue to be valid and not in 404 was not found, such as the original http: / / ss jb51. net/bbs/tread - 60 html, let it continue to be valid under the new domain, Forwarded to after clicking http: / / bbs jb51. net/tread - 60 html, and other websites, such as the original http: / / ss jb51. net/purchase don't into the secondary domain name bbs jb51. net/purchase, but rather to www. jb51. net/purchase
To do so, the redirect rule should read:
 
RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/bbs/ 
RewriteRule ^bbs/(.*) http://bbs.jb51.net/$1 [R=permanent,L] 
RewriteCond %{REQUEST_URI} !^/bbs/ 
RewriteRule ^(.*) //www.jb51.net/$1 [R=permanent,L] 

3.Apache mod_rewrite rule rewrite flag list
 
1) R[=code](force redirect)  Force external redirection  
 Forces an addition to the substitution string http://thishost[:thisport]/ The prefix redirects to the external URL. if code If not specified, the default will be used 302 HTTP Status code.  
2) F(force URL to be forbidden) disable URL, return 403HTTP Status code.  
3) G(force URL to be gone)  mandatory URL for GONE To return to 410HTTP Status code.  
4) P(force proxy)  Force the use of proxy forwarding.  
5) L(last rule)  Indicates that the current rule is the last rule, and stops the rewriting of the rule after the analysis.  
6) N(next round)  Re-run the rewrite process from the first rule.  
7) C(chained with next rule)  Associated with the next rule  

This flag is invalid if the rule matches, and if it does not, then all the associated rules below are skipped.
 
8) T=MIME-type(force MIME type)  mandatory MIME type  
9) NS (used only if no internal sub-request)  Only for non-internal subrequests  
10) NC(no case)  Case insensitive  
11) QSA(query string append)  Append request string  
12) NE(no URI escaping of output)  No escaping special characters in the output  
 Such as: RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE]  Will be able to correct will /foo/zoo Converted to /bar?arg=P1=zoo 
13) PT(pass through to next handler)  Pass it to the next process  
 Such as:  
RewriteRule ^/abc(.*) /def$1 [PT] #  Will be handed over to /def Rule processing  
Alias /def /ghi 
14) S=num(skip next rule(s))  skip num rule  
15) E=VAR:VAL(set environment variable)  Setting environment variables  

4.Apache rewrite example set

URL redirect

Example 1:
Meet the following two requirements at the same time:
In http: / / www zzz. com/xxx php to access http: / / www zzz. com/xxx/
. 2. Use http: / / yyy zzz. com to access http: / / www zzz. com/user php? username=yyy function
 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.zzz.com 
RewriteCond %{REQUEST_URI} !^user.php$ 
RewriteCond %{REQUEST_URI} .php$ 
RewriteRule (.*).php$ http://www.zzz.com/$1/ [R] 
RewriteCond %{HTTP_HOST} !^www.zzz.com 
RewriteRule ^(.+) %{HTTP_HOST} [C] 
RewriteRule ^([^.]+).zzz.com http://www.zzz.com/user.php?username=$1 

Example 2:

1 / type php? typeid = * � > /type*.html
1 / type php? typeid = * & page = * � > /type*page*.html
 
RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT] 
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT] 

5. Configure the multi-user virtual server
using URL Rewrite of Apache
To do this, first open the domain name resolution on the DNS server (either by yourself or with the domain name service provider). For example, I resolved *.kiya.us and *.jb51.net to my IP address of 70.40.213.183.

Then, take a look at my Apache Settings for the *.kiya.us virtual host.
 
ServerAdmin webmaster@kiya.us 
DocumentRoot /home/www/www.kiya.us 
ServerName dns.kiya.us 
ServerAlias dns.kiya.us kiya.us *.kiya.us 
CustomLog /var/log/httpd/osa/access_log.log "  common 
ErrorLog /var/log/httpd/osa/error_log.log "  
AllowOverride None 
Order deny,allow 

#AddDefaultCharset GB2312 

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^[^.]+.kiya.(cn|us)$ 
RewriteRule ^(.+) %{HTTP_HOST}$1 [C] 
RewriteRule ^([^.]+).kiya.(cn|us)(.*)$ /home/www/www.kiya.us/sylvan$3?un=$1&%{QUERY_STRING} [L] 

In this set, I put the *. jb51. net and *. kiya. The us Document Root are set to/home www/www kiya. us

Moving on, I've configured the URL Rewrite rule here.
 
RewriteEngine on # Open the URL Rewrite function  
RewriteCond %{HTTP_HOST} ^[^.]+.kiya.(cn|us)$ # Match the condition if the user enters it URL The host name is similar  xxxx.kiya.us  or  xxxx.jb51.net  Just execute the following sentence  
RewriteRule ^(.+) %{HTTP_HOST}$1 [C] # Enter the user's full address ( GET Passed as a parameter to the next rule, [C] is Chain Concatenate the meaning of the next rule  
RewriteRule ^([^.]+).kiya.(cn|us)(.*)$ /home/www/dev.kiya.us/sylvan$3?un=$1&%{QUERY_STRING} [L] 
#  The key is this sentence, the use of the expression to parse the user input URL Address, using the username information in the host name as the name un Parameter passed to /home/www/dev.kiya.us  Scripts in the directory and follow the user input later GET The incoming parameter of the method. And indicate that this is the last rule ( [L] Rules). Note that the rewritten address specified in this sentence USES the absolute path on the server, which is an internal jump. If you are using http://xxxx such URL Format is called an external jump. Use external jumps to browse in the browser URL The address is changed to the new address, and the address in the browser is not changed using an internal skip, which looks more like the actual secondary virtual server.  

Restart the Apache server after setting it up and you're done!
Update May 1, 2009

Today I went online and saw someone ask me a question:

Find Rewrite anti-hotlinking regular
. Don't allow www. im286. com www chinaz. com hotlinking the two website, other sites can hotlinking rules how to write.

The answer in the forum is:
 
RewriteEngine On 
RewriteCond %{HTTP_REFERER} chinaz.com [NC] 
RewriteCond %{HTTP_REFERER} im286.com [NC] 
RewriteRule .*\.(jpg|jpeg|gif|png|rar|zip|txt|ace|torrent|gz|swf)$ http://www.xxx.com/fuck.png [R,NC,L] 

Update May 7, 2009

Introduce an article: http: / / lamp linux. gov. cn/Apache/ApacheMenu/mod/mod_rewrite html

Update May 24, 2009

I. on whether it is necessary to use full escape, for example,
%{HTTP_REFERER} chinaz.com [NC] changes chinaz.com to chinaz\.com
The answer is both.

Ii. Today, when I was making YOURcaddy.com (which is the deformation of PlanetCoachella I made last year), I couldn't steer properly on the GoDaddy main engine On HostMonster and my own machine,
RewriteRule ^business/([^\.]+)$ biz/detail.php?name=$1 [L]
To achieve the rewrite. On the Godaddy host, it looks like this:
RewriteRule ^business/([^\.]+)$ /biz/detail.php?name=$1 [L]
There is one more/
before the target file Now I think it may be because RewriteBase is not specified, as for whether I will check it another day.

3. Add two examples about judging USER AGENT and automatically adding.php extension and automatically changing.html to.php extension:
1

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC]
The "-" here means no substitution. Visitors with IE and Opera browsers will be banned.

2

RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ([^/]+)$ /test/$1.php
#for example: /test/admin = > /test/admin.php
RewriteRule ([^/]+)\.html$ /test/$1.php [L]
#for example: /test/admin.html = > /test/admin.php

Limit the directory to show only images
< IfModule mod_rewrite.c >
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|swf)$
*$pool [F,L]
< /IfModule >

Update Jun 10, 2009

In addition, overrides for specific file extensions.

Overwrite files with certain extensions:
RewriteRule (.*.css$|.*.js$) gzip.php?$1 [L]
If you want to exclude some extensions:
RewriteRule !\.(js|ico|gif|jpg|JPG|png|PNG|css|pdf|swf)$ index.php

Related articles: