Home  >  Article  >  php教程  >  Apache URL rewriting

Apache URL rewriting

黄舟
黄舟Original
2016-12-21 11:21:212551browse

The first method: If you want to rewrite the URL address in the Apache environment, regular expressions are the most basic requirement, but for general URL addresses, basic matching can meet most of our requirements, so unless it is very Special URL addresses, but this is not the scope of what I want to discuss. Learn how to rewrite URL addresses in Apache in a few simple steps. Through examples, you can easily learn to rewrite URL addresses:

002

003 URL examples

004

005 Rewrite URL: http://www.baidu.com/?p=152

006

007 Original URL: http://www.baidu.com/p152.html

008

009 Rewrite rules:

010

011 ^p([0-9]+).html /?p=$1 [L]

012

013 Basic regular knowledge:

014

015 ^ Match the beginning of the line, match the URL The beginning of the address. For RewriteRule, the domain name (http://www.xxxx.com) is not part of the URL address, as above:?p=152

016

017 () separates a captured expression, As above: ([0-9]+)

018

019 [] Define character class, as above: [0-9] represents numbers from 0-9

020

021 + Indicates that the previous character can be repeated Match 1 or several times, as above: [0-9]+, indicating any number combination

022

023 Character escape, as above: escape.

024

025 Others:

026

027 [ L] means last, stop matching other

028

029 The method is as follows:

030

031 1. Open the httpd.conf file and find

032

033 #Lo adModule rewrite_module modules/mod_rewrite.so comment in front #

034

035 2. Open the httpd-vhosts.conf file and add rewrite rules in VirtualHost,

036

037 RewriteEngine On

038

039 RewriteRule ^p([0-9]+).html / ?p=$1 [L]

040

041 Basically these are the two steps above. In fact, in general, URL address rewriting in Apache is relatively simple, and it is much faster than learning from the documentation, but it requires If you want to know more about it, it is necessary to read the relevant documents. Other rules can be customized. Remember: any match is actually a regular expression replacement process.

042

043 Creating search engine-friendly URL addresses is very important for PHP programmers, so simply learning URL address rewriting in Apache will be one of the most basic requirements.

044

045

046 Second method:

047

048 1. First check whether the rewrite module is installed:

049

05 0 cat httpd.conf | grep rewrite

051 LoadModule rewrite_module modules/ mod_rewrite.so

052

053 2. Generate pseudo-static html connection:

054

055 (1) Generate pseudo-static html

056

057 Add

058

059 at the end of the section RewriteEngine on

060 RewriteRule /goods([0-9]+).html /goods.php?id=$1 [PT]

061 A more standard way of writing is:

062 RewriteRule ^(.*)/goods( [0-9]+).html$ $1/goods.php?id=$2 [PT]

063 More concise writing:

064 /goods(d+).html /goods.php?id=$1

065

066 The first (0-9]+) corresponds to parameter $1, and so on the second one corresponds to $2

067

068 Example:

069

070 RewriteRule /forum- ([0-9] +)-([0-9]+).html /forumdisplay.php?fid=$1&page=$2 [PT]

071

072 Test whether http://www.xxx.com/goods1.html is consistent with / The content of goods.php?id=1 is the same

073

074 Finally, all links are replaced with the set pseudo-static html address

075

076

077 [PT]: URL global conversion, that is, the converted goods31.html corresponds to goods.php?id=31 (the default is this without parameters)

078 [R]: URL redirection is when accessing goods31.html Jump to goods.php?id=31

079

080

081 3. Anti-hotlink:

082

083 RewriteCrond %{HTTP_HOST} !xxxx.com [R =301,L]

084 RewriteRule ^(.*)$ http://www.xxxx.com/warning.html [R=301,L]

085

086 Redirect requests not from xxxx.com to http://www.xxxx .com

087

088 Better approach:

089 RewriteCond %{HTTP_REFERER} !^http://(www.)?xxxx.com/.*$ [NC]

090 RewriteRule .(mp 3| rar|jpe|gif)$ http://www.xxxx.com/warning.jpg [R=301,L]

091

092 4. Anti-Baidu crawler:

093 RewriteCond %{HTTP_USER_AGENT} ^Baiduspider [ OR]

094 RewriteRule ^(.*)$ http://www.google.com [R=301,L]

095 Transfer the crawler from Baidu to goole

096

097

098

099

100 PS: PHP pseudo-static method

101

102 Method 1:

103

104 For example, this webpage

105

106 http://www.xxxx.com/soft.php/1,100, 8630.html

107

108 In fact, the script processed is soft.php and the parameters are 1,100,8630

109

110 Equivalent to soft.php?a=1&b=1=100&c=8630 It’s just like this URL is too difficult remember. Search engines don’t like it either.

111

112 True static is just completely generated HTML.

113

114 Directly output when the client accesses. No need for script explanation. It will have very good results when the traffic is very large (such as when there are millions of visits every day). In other words, this HTML page actually exists on the server side.

115

116 Of course, when the traffic of your website is not that large. URL rewriting is the best method (in my personal opinion, you can consider load balancing when there is large traffic. It doesn't matter either)

117

118 There are many methods of URL rewriting, APACHE, IISREWRITE. Even PHP scripts can handle it directly. For example, in the above example, the PHP script handles it directly (the advantage of this method is that it directly reduces the pressure on the WEB server when there is a large amount of traffic. PS: This is also a personal opinion:

119

120 =========== =====================================

121

122 Let’s take the program as an example PHP pseudo-static program implementation method. In fact, I have posted this method in other forum communities before

123

124 Program example:

125

126 http://www.xxxx.com/soft.php /1,100,8630.html

127

128 CODE:

129

130 //Use the server variable to obtain the PATH_INFO information. In this example, it is /1,100,8630.html, which is the part after the execution script name

131

132 if(@$path_info =$ _SERVER["PATH_INFO"]){

133 //Regular match parameters

134 if(preg_match("//(d+),(d+),(d+).html/ si",$path_info,$arr_path)){

135 $gid =intval($arr_path[1]); //Get the value 1

136 $sid =intval($arr_path[2]); //Get the value 100

137 $softid =intval($arr_path[3]); //Get the value 8630

138 }else die("Path:Error!");

139 //Equivalent to soft.php?gid=1&sid =100&softid=8630

140 //It’s that simple. ~)

141 Method 2:

142 Open Apache’s configuration file httpd.conf.

143 2 Remove the # in front of #LoadModule rewrite_module modules/mod_rewrite

144 3 Add in httpd.conf:

145

146 RewriteEngine On

147 #RewriteCond %{ENV:SCRIPT_URL} (?:index|dispbbs)[-0-9]+.html

148 RewriteRule ^(.*?(?:index|dispbbs))-([-0-9 ]+).html 1.php?__is_apache_rewrite=1&__rewrite_arg=2

149  

150   Fourth, to realize the mapping of asp post URL to php post, in the third step, and Add:

151 RewriteMap tolowercase int:tolower

152 RewriteCond %{QUERY_STRING} (?:boardid|page|id|replyid|star|skin)=d+ [NC]

153 RewriteRule ^ (.*(?:index|dispbbs)).asp 1.php?{tolowercase:%{QUERY_STRING}}&__is_apache_rewrite=1

154 5 Save httpd.conf and restart Apache

155 Method 3:

156 < ?php

157 function mod_rewrite(){

158 global

159 $ _GET;

160 $nav=$ _SERVER["REQUEST_URI"];

161 $script_name=$ _SERVER["SCRIPT_NAME"];

162 $nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);

163 $nav=preg_replace("/^.ht(m){1}(l){ 0,1}$/","",$nav);//This sentence is .html or .htm with the tail removed

164 $vars = explode("/",$nav);

165 for($ i=0;$i

166 $ _GET["$vars[$i]"]=$vars[$i+1];

167 }

168 return $ _GET;

169 }

170 mod_rewrite();

171 $yearn=$ _GET["year"];//The result is '2006'

172 $action=$ _GET["action"] ;//The result is '_add'

173 echo $yearn;

174 echo $action;

175 ?>

176

177 function mod_rewrite(){

178 global $ _GET;

179 $nav= $ _SERVER["REQUEST_URI"];

180 $script_name= $ _SERVER["SCRIPT_NAME"];

181 $nav=substr(ereg_replace("^$script_name","",url decode($ nav)),1);

182 $nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//This sentence is Remove the trailing .html or .htm

183 $vars = explode("/",$nav);

184 for($i=0;$i

185 $ _GET["$vars[$i]"]=$vars[$i+1];

186 }

187 return

188 $ _GET;

189 }

190 mod_rewrite();

191 $yearn= $ _GET["year"];//The result is '2006'

192 $action=$ _GET["action"];//The result is '_add'

193 echo $yearn;

194 echo $action;

195 In many cases, the access of a certain IP can easily cause the CPU to be 100% (such as fixed crawling by some search engines, a large number of collection sites by others), at this time we have to use some effective The method is to block the other party's IP so that it cannot consume the server's resources. There are many ways to block IP. If your Web server has the Rewrite module installed, you can also try to use Rewrite rules to block the other party's IP.

196 1. For example, we redirect a specific IP directly to the baidu homepage and add the code in the .htaccess file in the root directory of the website:

197 RewriteCond % 123.123.123.123 [NC]RewriteRule ^(.*)$ http://www.baidu.com/$1 [R=301] Replace the IP 123.123.123.123 with the IP you want to restrict

198 2. If you want to implement multiple IPs, you can write like this:

199 RewriteCond % 123.123.123.123 [OR]RewriteCond % 124.124.124.124 [NC]RewriteRule ^(.*)$ http://www.baidu.com/$1 [R=301]

The above is the URL address rewritten by Apache Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Apache memory release~Next article:Apache memory release~