What is PHP Redirect?
Redirection is the redirection of various network requests to other locations through various methods (such as web page redirection, domain name redirection, and routing changes. Targeting).
- Chinese name
- Redirect
- Foreign name
- Redirect
- To do
- Transfer various network requests to other locations
- E.g
- Web page redirection, domain name redirection
- Needed
- Website adjustments,
- the way
- 301 redirect, 302 redirect
- Other orientation
- Linux redirect
- Redirection is the redirection of various network requests to other locations through various methods (such as web page redirection, domain name redirection, and changes in routing choices. Targeting).
Redirect webpage targeting
- In our website construction, we often encounter situations that require web page redirection:
- 1. Website adjustment (such as changing the directory structure of web pages);
- 2. The webpage is moved to a new address;
- 3. The extension of the web page is changed (such as the application needs to change .php to .Html or .shtml).
- In this case, if redirection is not performed, the old address in the user's favorites or search engine database will only allow the visiting customer to get a 404 page error message, and the visit traffic will be lost; furthermore, some websites registered with multiple domain names , You also need to redirect users who visit these domains to the main site automatically by redirecting.
Redirection details
- Common redirection methods are: 301 redirect, 302 redirect, and meta fresh.
- 301 redirect
- 301 stands for Permanently Moved. 301 redirection is the best way for search engines to be friendly after a webpage changes its address. As long as it is not a temporary move, it is recommended to use 301 for forwarding.
- 302 redirect
- 302 stands for Temporarily Moved. In the past few years, many Black Hat SEO (black hat SEO) have widely used this technology to cheat. At present, major search engines have strengthened their crackdown, like Google s previous ones. Punishment of the domain name Business (Business) and the recent BMW Germany website. Even if the website is not a spam objectively, it is easy for a search engine to easily misjudge it as a spam and be punished.
- meta fresh
- This was more popular 2000 years ago, but it is rare now. It is specifically redirected to a new web page after a certain time through the meta instruction in the web page. If the delay time is too short (within about 5 seconds), it will be judged as spam.
Redirect method
- 301 Redirect
- IIS server implements 301 redirect
- * Open the internet information service manager and right-click on the webpage or directory you want to redirect
- * Select "Redirect to URL"
- * In the "Redirect to" input box, enter the URL of the target webpage
- * Check "Permanent redirection of resources" (remember)
- * Finally click "Apply"
- Apache server implements 301 redirect
- In comparison, Apache is much simpler to implement than IIS. In Apache, there is a very important file .htaccess. By setting it, you can achieve many powerful functions. 301 redirection is just one of them.
- Redirect permanent / http://www.bloghuman.com/ (Redirect the contents of the directory to http://www.bloghuman.com/)
- redirect permanent /index.php http://www.bloghuman.com/index.php?go=category_6 (redirect page index.php to http://www.bloghuman.com/index.php?go=category_6)
- By properly configuring regular expressions in the redirection parameters, more complex matching can be achieved. Interested friends can refer to the Apache manual of this site.
- 301 redirect under PHP
- <? php
- Header ("HTTP / 1.1 301 Moved Permanently");
- Header ("Location: http://www.bloghuman.com");
- ?>
- 301 redirect under ASP
- <% @ Language = VBScript%>
- <%
- Response.Status = "301 Moved Permanently"
- Response.AddHeader "Location", "http://www.bloghuman.com"
- %>
- 301 redirect under ASP.NET
- <script runat = "server">
- private void Page_Load (object sender, System.EventArgs e)
- {
- Response.Status = "301 Moved Permanently";
- Response.AddHeader ("Location", "http://www.bloghuman.com");
- }
- </ script>
- 301 redirect under ColdFusion
- <.cfheader statuscode = "301" statustext = "Moved permanently">
- <.cfheader name = "Location" value = "http://www.new-url.com">
- Old domain to new domain
- Create a .htaccess file and write the code provided below into the file. It can ensure that all directories or web pages of the old domain name are correctly redirected to the new domain name.
- Remember that the .htaccess file must be placed in the root directory of the old website, and the new website must maintain the same directory structure and web files as the old website.
- Options + FollowSymLinks
- RewriteEngine on
- RewriteRule (. *) Http://www.domain.com/$1 [R = 301, L]
- Please modify www.domain.com above to the domain name you want to redirect to.
- In addition, it is recommended that everyone return to the external link of the old website and contact the corresponding site to modify the URL of the import chain to point to the new site.
- Domain 301 redirects to the secondary domain name of www
- I have repeatedly mentioned in the previous article: www.domain.com is a second-level domain name under the domain.com domain, and made a detailed description of how to choose between a second-level domain name and a first-level directory and optimize the website structure Analysis, those who are interested can read two articles. Website structure optimization is the most important in SEO.
- OK, continue the implementation of domain 301 to www secondary domain:
- Create a .htaccess file and write the code provided below into the file. It will ensure that all requests from domain.com are forwarded to www.domain.com.
- Options + FollowSymlinks
- RewriteEngine on
- rewritecond% {http_host} ^ domain.com [nc]
- rewriterule ^ (. *) $ http://www.domain.com/$1 [r = 301, nc]
- Please change the domain.com and www.domain.com above to the domain name you want to redirect to.
- Note: .htaccess files will only work on Linux hosts using Apache with the Mod Rewrite module installed as a web server
Redirection detection tool
- After the configuration is complete, be sure to check whether it is correct. There are many similar Server Header checking tools on the Internet, such as Check Server Headers Tool-HTTP Status Codes Checker
- Improper use of website 302 redirect leads to GOOGLE punishment
- Business.com is the largest business search engine and classified directory on the Internet. It is well-known for providing professional business information, including nearly 190,000 web pages. If you search on Google with the word "business" as the keyword, the site ranks first. However, on September 5, Business.com encountered a strange thing: its home page changed from PR8 to PR0, and the home page could not be found in Google search results. Fortunately, only the homepage was "evaporated", but thankfully the homepage returned to Google search results the next day, but the PR was still 0.
- Business.com's problem is with its redirect command. The site redirected business.com to www.business.com, and this redirection should have been permanent. We know that 301 is a permanent redirect, and 302 is a temporary redirect. You can use this command only when a website or webpage is temporarily moved to another location within 24 to 48 hours. But Business.com incorrectly used the "HTTP / 1.1302 Object Moved" status code.
- common case
- In fact, website redirection is extremely common, such as applying for a new domain name if you are not satisfied with the original domain name; buying a domain name that is easy to be misspelled to prevent customers from finding the website because they misspell the URL, etc. However, many people will be "disaster" because of using the wrong redirection status code, just like Business.com. Although their redirection reasons are reasonable, if they are not used properly, they may be mistaken for Google to use multiple domain names to point to the same website, then your website will be blocked. The charge is "using duplicate content to interfere with Google search Resulting Website Ranking. " Business.com is the best lesson learned. It's just that most websites that use the wrong redirect parameters are not as lucky as Business.com. A small redirect may make the website useless, and it can only be done from scratch: re-apply for a new domain name, re-publish a new website, and so on. Remember: Google will never sympathize with anyone, even if they make mistakes inadvertently.
Redirect operation process
- Operation process of website server in response to web browsing request
- Let's take a look at the initial interaction process between users / search engines and websites. When a user or search engine makes a web browsing request to a web server, the server will:
- 1. Convert the domain name to the IP address of the website through the domain name server (DNS) and return it to the customer
- 2. Open an IP socket connection
- 3. Make a note of an HTTP data stream through the socket
- 4. Receive an HTTP data stream from the web server in response to the request. The data stream contains a status code whose value is determined by the HTTP protocol. The "HTTP data stream" information referred to here is also called "Header". The header includes the date, server type, and usually a "200 OK" message. If all is well, the web server will send the "200 OK" message along with the request page. If the website has already established a redirect at this time, the server will include a response message such as "302 Moved Temporarily" or "301 Moved Permanent" in the header. Search engines make decisions based on what's in the server header.
Redirection considerations
- Considerations for website redirects
- 1. If you plan to move the files on the server to another place, you must properly notify the crawler of the search engine about the following information:
- -Destination: where these files were moved
- -Move property: remove temporarily or permanently
- 2. For websites with multiple domain names, experts recommend that those domain names that do not want to be promoted on search engines should be redirected permanently with the 301 redirect command.
- Make sure the website implements the correct 301 redirect
Redirect instruction implementation
- For correct implementation of 301 redirects
- 1.Add 301 redirect instruction in .htaccess file
- 2. Suitable for users using Unix web server. Use this command to inform search engine spiders that your site files are not at this address. This is the more common method. In the form: Redirect 301 / http://www.yourdomain.com/
- 3. Complete the 301 redirect in the system administrator configuration area of the server software
- For users using Window Web Server
- 4. Using "mod_rewrite" technology
- Changes made through this technology will be reflected in the .htaccess file, which looks like this:
- Options + FollowSymLinks
- RewriteEngine on
- RewriteCond% {HTTP_HOST} ^ yourdomain.com
- RewriteRule ^ (. *) $ Http://www.yourdomain.com/$1 [R = permanent, L]
- 5. Binding / Local DNS (Domain Name Aliasing)
- If you have the right to edit and modify local DNS records, you can solve this problem by simply adding a record.
- Without this permission, you can ask the web hosting service provider to set the DNS server accordingly.
- DNS server settings
- To point aaa.domain.com to www.domain.com, you only need to add an alias record in the DNS service, which can be written as: aaa IN CNAME www.domain.com.
- If you need to configure a large number of virtual domain names, you can write: * IN CNAME www.domain.com.
- This will redirect all unset records ending in domain.com to www.domain.com.
- 6. 301 redirect with ASP / PHP:
- The code has been introduced above
Redirect expert opinion
- Views and recommendations from senior SEO expert Dan Thies
- Dan Thies experience
- Dan Thies is well aware of the problems encountered by Business.com, as he has had similar experiences. There is a member tracking script on his website. One member's site is mapped to this tracking script through 302 command, and this tracking script is mapped to his homepage through 302. When searching for "keyword research" in Google, his homepage ranks in the top ten, but the address shows the member's web address. The result made him laugh and cry: The visitor entered his website through Google search results, but he had to pay the member for those visits! Later, he used robots.txt to prevent Spiders from tracking his member tracking script. Something went wrong.
- As for Business.com, Dan Thies believes that "there is still some problem with Google's handling of 302 redirects", but it does not mean that Google does not allow 302 redirects. Business.com was not blocked or punished, they just returned incorrect responses. "
- Dan Thies suggests
- If you use a tracking URL / script and you have to redirect the visitor to a certain landing page, then you must disable Spiders in the robots.txt file to access the second redirect. If you do not redirect the tracking URL / script, but just copy the content from another URL, you should prevent Spiders from accessing the tracking URL in the robots.txt file to prevent the search engine from being punished for duplicate content.
Redirection experience
- 301 redirect experience
- A multi-domain website owner's experience
- "I only have one website, the main domain name is www.domain.com, and there are more than a dozen domain names such as domain1.com, domain2.com, domain3.com, etc. All these secondary domain names are mapped to www.domain.com And, all domain names correspond to the same IP address. Due to the fact that I have only one site, and one site can only correspond to one .htaccess file, it is obviously impossible to modify the .htaccess file directly to implement redirection.
- Step flow
- A. Separate domain1.com from domain.com and make it an ordinary independent website on the server.
- B. Create a .htaccess text file for domain1.com and set the redirect code in the file to:
- Redirect permanent / http://www.domain.com/
- Upload the modified .htaccess file to the server where domain1.com is located.
- This step can also be done in the domain control panel.
- C. Enter the domain name control panel, release all the domain names originally bound to domain.com from domain.com, and then rebind to doamin1.com. In this way, these bound domain names use the .htaccess file of domain1.com, which means that they are permanently redirected to domain.com. Alright, you're done! It's that easy!
Redirect Baidu Encyclopedia
Redirect synonym jump
- In the past, when creating or retrieving a person's entry in Baidu Encyclopedia, it was usually based on the person's common name or our well-known and familiar name, such as football star David Beckham. "David Beckham", but under Baidu Encyclopedia's merged synonym rule, more standardized or commonly used terms (or meanings) are used as synonyms between synonyms. For the convenience of readers, Baidu Encyclopedia introduced the synonym jump function (also known as redirection).
- After the synonym jump function is launched, as long as you search for "Beckham" in Baidu Encyclopedia, or click on other entry links with "Beckham", it will directly jump to "David Beckham", and At the top of the entry, it will be marked "The jump to this entry has been made for you. The exact match is Beckham ."
- Unlike the synonym jump, clicking "Beckham" here will enter the "Beckham" entry which is still editable.
Retargeting
- This redirection function introduced by Baidu Encyclopedia is particularly applicable to foreign person entries, such as US President Barack Obama. Searching or clicking on "Obama" will redirect to "Barack Obama", searching or clicking on "Schwarzenegger" will Orientation to Arnold Schwarzenegger.
- Not only that, the emperor and writer's entries also used redirection, such as "Xunzheng" redirected to "Qin Shihuang", "Dongpojushi" redirected to "Su Shi", and "Zhou Shuren" redirected to "Lu Xun".
- Taiwanese girl music group SHE has argued about the use of standard name jumps in Baidu Encyclopedia. Fans think that "SHE" is the standard word, but "SHE" means "her", and there are songs of the same name. Therefore the two cannot be merged into synonyms. Redirection solves this problem very well. After setting the redirect, just search or click on "SHE" to redirect to "SHE". The information about the music combination is in the "SHE" entry, and the original "SHE" "Entries are streamlined to related explanations and songs of the same name.
Redirect Wikipedia
- Redirects are also available in Wikipedia, but redirects in Wikipedia
- Redirects to search for "USA" in Wikipedia
- This type of redirection is very common in Wikipedia, as long as articles with consent to association are set up for redirection.
Redirecting other orientations
- Linux redirect
Basic redirection concepts
- a, I / O redirection is usually related to FD, shell FD is usually 10, that is, 0-9;
- b. There are 3 commonly used FDs, which are 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error output). The default is related to keyboard, monitor, and monitor;
- c. Use <to change the read data channel (stdin) to make it read from the specified file;
- d. Use> to change the data channel (stdout, stderr) sent to the specified file;
- e, 0 are the default values of <, so <is the same as 0 <; similarly,> and 1> are the same;
- f. In IO redirection, the pipes of stdout and stderr will be prepared before reading data from stdin;
- g, pipe "|" (pipe line): stdout of the previous command received stdin of the next command;
- h, tee command is to copy stdout to the file without affecting the original I / O;
- i. The process of bash (ksh) executing the command: analyzing the command-variable evaluation-command substitution (`` and $ ())-redirection-wildcard expansion-determining the path-executing the command;
- j. () Put the command group in a sub-shell for execution, also called nested sub-shell. It has one very important feature: inheriting the standard input, output, and error plus any other open file descriptors of the parent shell.
- k, exec command: Used to replace the current shell and restart a shell, in other words, no subshell is started. Any existing environment will be cleared when using this command. When exec operates on file descriptors, only then does exec not overwrite your current shell environment.
IO Redirect basic IO
- cmd> file redirect stdout to file;
- cmd >> file redirect stdout to file (append);
- cmd 1> fiel redirect stdout to file;
- cmd> file 2> & 1 redirect stdout and stderr to file together;
- cmd 2> file redirects stderr to file;
- cmd 2 >> file redirect stderr to file (append);
- cmd >> file 2> & 1 redirect stdout and stderr to the file (append);
- cmd <file> file2 The cmd command uses the file file as stdin and the file2 file as stdout;
- cat <> file opens file for reading and writing;
- cmd <file cmd command uses file file as stdin;
- cmd << delimiter Here document, read from stdin until the delimiter delimiter is encountered.
IO Redirection Advanced IO
- > & n Use system call dup (2) to copy file descriptor n and use the result as standard output;
- <& n Standard input copied from file descriptor n;
- <&-Close standard input (keyboard);
- > &-Close standard output;
- n <&-means to close the n input;
- n> &-means to turn off output n;
- All the above forms can be preceded by a number, and the file descriptor created at this time is specified by this number instead of the default 0 or 1. Such as:
- ... 2> file runs a command and directs the error output (file descriptor 2) to file.
- ... 2> & 1 runs a command and merges its standard output and output. (Strictly speaking, file descriptor 2 is created by copying file descriptor 1, but the effect is usually to merge the two streams.)
- Let's elaborate on 2> & 1: 2> & 1 is FD2 = FD1. This is not to say that the value of FD2 is equal to the value of FD1, because> is to change the data channel sent, that is to say, the "data output channel" Change to "Data Output Channel" of FD1. If only this, this change seems to have no effect, because the default output of FD2 and the default output of FD1 are originally monitors, the same! However, when FD1 is another file or even another FD, this has a special purpose. Please understand this.
- exec 0exec 1> outfilename # Open the file outfilename as stdout.
- exec 2> errfilename # Open the file errfilename as stderr.
- exec 0 <&-# Close FD0.
- exec 1> &-# Close FD1.
- exec 5> &-# Close FD5.