ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄÖض¨ÏòºÍURLÖØд
ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄÖض¨ÏòºÍurlÖØд
NginxÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ËüÒ²ÌṩÁËÇ¿Ê¢µÄ¹¦Ð§À´ÖÎÀíHTTPÇëÇóµÄÖض¨ÏòºÍURLÖØд¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃNginxÉèÖÃÎļþÀ´ÊµÏÖÕâЩ¹¦Ð§£¬²¢Ìṩ´úÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£
HTTPÇëÇóÖض¨Ïò
HTTPÇëÇóÖض¨ÏòÊÇÖ¸½«Ò»¸öÇëÇó´ÓÒ»¸öURLÖض¨Ïòµ½ÁíÒ»¸öURL¡£ÕâÔÚÐí¶àÇéÐÎϺÜÊÇÓÐÓ㬺ñȵ±Ä³¸öÒ³Ãæ±»Òƶ¯µ½ÁËеÄURL£¬»òÕßÐèÒª½«HTTPÇëÇó´ÓÒ»¸öÓòÃûÖض¨Ïòµ½ÁíÒ»¸öÓòÃû¡£
ÏÂÃæÊÇÒ»¸öʹÓÃNginx¾ÙÐÐHTTPÇëÇóÖض¨ÏòµÄʾÀý£º
server { listen 80; server_name example.com; location /old-url { return 301 http://example.com/new-url; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬µ±Óû§»á¼ûexample.com/old-urlʱ£¬Nginx»á·µ»ØÒ»¸ö301Öض¨ÏòÏìÓ¦£¬²¢½«Óû§Öض¨Ïòµ½example.com/new-url¡£
URLÖØд
URLÖØдÊÇÖ¸½«Ò»¸öURLת»»³ÉÁíÒ»¸öURLµÄÀú³Ì¡£ÕâÔÚÐí¶àÇéÐÎϺÜÊÇÓÐÓ㬺ñÈΪÁËÌá¸ßÍøÕ¾µÄSEOÐÔÄÜ£¬»òÕßΪÁËÈÃURLÔ½·¢ÓѺá£
ÏÂÃæÊÇÒ»¸öʹÓÃNginx¾ÙÐÐURLÖØдµÄʾÀý£º
server { listen 80; server_name example.com; location /old-url { rewrite ^/old-url/(.*)$ /new-url/$1 last; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬µ±Óû§»á¼ûexample.com/old-url/xxx/yyyʱ£¬Nginx»á½«URLÖØдΪexample.com/new-url/xxx/yyy£¬²¢½«ÇëÇóת´ï¸øºó¶ËЧÀÍÆ÷¾ÙÐд¦Àí¡£
×éºÏʹÓÃÖض¨ÏòºÍURLÖØд
ͨ³£ÇéÐÎÏ£¬ÎÒÃÇ»áÐèҪͬʱʹÓÃÖض¨ÏòºÍURLÖØдÀ´ÊµÏÖһЩÖØ´óµÄÐèÇó¡£ÏÂÃæÊÇÒ»¸ö×éºÏʹÓÃÖض¨ÏòºÍURLÖØдµÄʾÀý£º
server { listen 80; server_name example.com; location /old-url { rewrite ^/old-url/(.*)$ /new-url/$1 last; } location /redirect-url { return 301 http://example.com/new-url; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬µ±Óû§»á¼ûexample.com/old-url/xxx/yyyʱ£¬Nginx»á½«URLÖØдΪexample.com/new-url/xxx/yyy£¬²¢½«ÇëÇóת´ï¸øºó¶ËЧÀÍÆ÷¾ÙÐд¦Àí¡£¶øµ±Óû§»á¼ûexample.com/redirect-urlʱ£¬Nginx»á·µ»ØÒ»¸ö301Öض¨ÏòÏìÓ¦£¬²¢½«Óû§Öض¨Ïòµ½example.com/new-url¡£
×ܽ᣺
ʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄÖض¨ÏòºÍURLÖØд¿ÉÒÔÌá¸ßÍøÕ¾µÄÐÔÄܺÍÓû§ÌåÑé¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃNginxÉèÖÃÎļþÀ´ÊµÏÖÕâЩ¹¦Ð§£¬²¢ÌṩÁË´úÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£Ï£Íû¶ÁÕßÄܹ»ÔÚÏÖʵµÄ¿ª·¢ÖÐÎÞаÔËÓÃÕâЩ¼¼ÇÉ£¬Îª×Ô¼ºµÄÍøÕ¾´øÀ´¸üºÃµÄЧ¹ûºÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄÖض¨ÏòºÍURLÖØдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡