Improved SEO with mod_rewrite
PHP Advanced and Object-Oriented Programming Third Edition
1 <?php 2 //D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess\mod_rewrite.php 3 4 echo '?w='.$_GET['w'].'&wb='.$_GET['wb']; 5 6 /* 7 http://localhost/0613pm/w_wwwroot/n/test_htaccess/mod_rewrite.php?w='cheer'&wb='time' 8 http://localhost/0613pm/w_wwwroot/n/test_htaccess/mod_rewrite.php?w=%27cheer%27&wb=%27time%27 9 ?w='cheer'&wb='time' 10 */ 11 12 /* 13 http://localhost/0613pm/w_wwwroot/n/test_htaccess/mod_rewrite.php?w=cheer&wb=time 14 ?w=cheer&wb=time 15 */ 16 17 /* 18 D:\wamp64\bin\apache\apache2.4.18\conf 19 20 # 21 # Deny access to the entirety of your server's filesystem. You must 22 # explicitly permit access to web content directories in other 23 # <Directory> blocks below. 24 # 25 <Directory /> 26 AllowOverride none 27 Require all denied 28 </Directory> 29 30 31 */ 32 33 /* 34 D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess 35 新建 .htaccess 36 37 #2016/8/13-11:11 38 <IfModule mod_rewrite.c> 39 #the .htaccess file mustfirst check for the module and turn on the rewrite engine: 40 RewriteEngine on 41 #After enabling the engine, and before the closing IfModule tag, you add rules dictating the rewrites. 42 #The syntax is RewriteRule match rewrite 43 RewriteRule w.php dearchild.php 44 RewriteRule ^rewrite_ok/\wzero/\wfirst$ mod_write.php?w=$1&wb=$2 45 </IfModule> 46 47 48 apache无法启动-0 49 #2016/8/13-11:04 50 <Directory 'D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess' /> 51 AllowOverride All 52 <Directory> 53 apache无法启动-1 54 #2016/8/13-11:04 55 <Directory 'D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess' /> 56 AllowOverride All 57 </Directory> 58 apache无法启动-2 59 #2016/8/13-11:04 60 <Directory "D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess" /> 61 AllowOverride All 62 <Directory> 63 apache启动 64 #2016/8/13-11:04 65 <Directory "D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess"> 66 AllowOverride All 67 <Directory> 68 TOOLS- Check http.conf syntax 逐步排错 69 70 */ 71 72 /* 73 D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess\w.php 74 任意 75 D:\wamp64\www\0613pm\w_wwwroot\n\test_htaccess\dearchild.php 76 <?php 77 echo 'dear_child_w'; 78 http://localhost/0613pm/w_wwwroot/n/test_htaccess/w.php 79 80 */ 81 echo 'dear_child_w';//dear_child_w 82 //验证通过 83 /* 84 RewriteRule somepage.php otherpage.php 85 With that code, any time a user goes to somepage.php, the user will be shown the contents ofotherpage.php, although the browser will still display somepage.php in the address bar. 86 To be clear, that’s not actually a good use of mod_rewrite, as a literal redirection could beaccomplished with the less complex mod_alias module instead. However, a line like that doesrepresent an easy way to test that mod_rewrite is working. 87 */ 88 89 /* 90 http://localhost/0613pm/w_wwwroot/n/test_htaccess/mod_write.php?w=wchild&wb=wchildb 91 Not Found 92 93 The requested URL /0613pm/w_wwwroot/n/test_htaccess/mod_write.php was not found on this server. 94 95 Apache/2.4.18 (Win64) PHP/5.6.19 Server at localhost Port 80 96 97 */ 98 99 /* 100 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/wzero/wfirst 101 Not Found 102 103 The requested URL /0613pm/w_wwwroot/n/test_htaccess/mod_write.php was not found on this server. 104 105 Apache/2.4.18 (Win64) PHP/5.6.19 Server at localhost Port 80 106 107 #2016/8/13-11:44 108 <IfModule mod_rewrite.c> 109 #the .htaccess file mustfirst check for the module and turn on the rewrite engine: 110 RewriteEngine on 111 #After enabling the engine, and before the closing IfModule tag, you add rules dictating the rewrites. 112 #The syntax is RewriteRule match rewrite 113 RewriteRule w.php dearchild.php 114 RewriteRule ^rewrite_ok/\wzero/\wfirst$ mod_rwrite.php?w=$1&wb=$2 115 </IfModule> 116 117 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/wzero/wfirst 118 119 ?w=&wb= 120 //get空,继续修正 121 */ 122 123 /* 124 #2016/8/13-11:57 125 <IfModule mod_rewrite.c> 126 RewriteEngine on 127 RewriteRule w.php dearchild.php 128 RewriteRule ^rewrite_ok/\w{1,}/\w{1,}$ mod_rewrite.php?w=$1&wb=$2 129 </IfModule> 130 131 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/sd 132 //?w=&wb= 133 134 135 #2016/8/13-11:58 136 <IfModule mod_rewrite.c> 137 RewriteEngine on 138 RewriteRule w.php dearchild.php 139 RewriteRule ^rewrite_ok/(\w{1,})/(\w{1,})$ mod_rewrite.php?w=$1&wb=$2 140 </IfModule> 141 142 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/sd 143 //?w=2er32&wb=sd 144 145 进一步研究-0 146 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32 147 //Not Found 148 149 The requested URL /0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32 was not found on this server. 150 151 Apache/2.4.18 (Win64) PHP/5.6.19 Server at localhost Port 80 152 153 进一步研究-1 154 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/t/ 155 //Not Found 156 157 The requested URL /0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/t/ was not found on this server. 158 159 Apache/2.4.18 (Win64) PHP/5.6.19 Server at localhost Port 80 160 161 考虑用户习惯,优化正则, 162 163 #2016/8/13-12:05 164 <IfModule mod_rewrite.c> 165 RewriteEngine on 166 RewriteRule w.php dearchild.php 167 RewriteRule ^rewrite_ok/(\w{1,})/(\w{1,})/{0,}$ mod_rewrite.php?w=$1&wb=$2 168 </IfModule> 169 170 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/t/ 171 //?w=2er32&wb=t 172 173 a-beautiful-world 174 175 #2016/8/13-12:07 176 <IfModule mod_rewrite.c> 177 RewriteEngine on 178 #The syntax is RewriteRule match rewrite 179 RewriteRule w.php dearchild.php 180 RewriteRule ^rewrite_ok/(\w{1,})/(\w{1,})/{0,}/a-beautiful-world$ mod_rewrite.php?w=$1&wb=$2 181 </IfModule> 182 */ 183 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/ta-beautiful-world 184 //Not Found 185 186 The requested URL /0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/ta-beautiful-world was not found on this server. 187 188 Apache/2.4.18 (Win64) PHP/5.6.19 Server at localhost Port 80 189 190 http://localhost/0613pm/w_wwwroot/n/test_htaccess/rewrite_ok/2er32/t/a-beautiful-world 191 //?w=2er32&wb=t
//Search Engine Optimization
The modularized site as written has a number of benefits, starting with the fact that it will be easier to maintain and expand as needed.
//模块化站点 维护 扩展
mod_rewrite is a tool that lets you instruct the server that when the user goes to one URL, the server should provide another resource.
mod_rewrite makes use of regular expressions, so the matching pattern and resulting actual URL can be as complex as needed.
Two ways to change Apache's behavior
0-edit the primary Apache configuration file
1-create directory-specific files
// 全局配置文件 目录相关文件
The primary configuration file is httpd.conf, found within a conf directory, and it dictates how the entire Apache Web server runs(where the httpd.conf file is on your system will depend on many things). An .htaccess file(pronounced “H-T access”) is placed within a Web directory and is used to affect how Apache behaveswithin just that folder and subfolders.Generally speaking, it’s preferable to make changes in the httpd.conf file, since this file needs to be read only by the Web server each time the server is started. Conversely, .htaccess files must be readby the Web server once for every request to which an .htaccess file might apply. For example, if youhave www.example.com/somedir/.htaccess, any request to www.example.com/somedir/whatever requires reading the .htaccess file, as well as reading an .htaccess file that might exist in www.example.com/. On the other hand, in shared hosting environments, individual users are not allowed to customize the entire Apache configuration, but they may be allowed to use .htaccess to make changes that only affect their sites. And changes to the primary configuration file do require restarting Apache to take effect.
发问:
0-references to external resources cheated the browser! Much more we can do!