One website operation sectionTimeIn this case, the topic settings may change, which may lead to URL changes. The biggest problem with URL changes is that the previous connection becomes invalid, which has a poor user experience and affects the search engine's indexing of the website. Therefore, we must ensure that the previous URL is not invalid.
For example, previous newsPageThe URL is http://msplinks.com.cn/mdfodhrwoi8vbxkuy29tlzeymzq1ni5odg1s. We can easily deal with this requirement using URL routing.
Write two rules first:
- # Parse and generate a new news page url
- News_page:
- RegEx: '/news/([A-Z] +)/([0-9] +) \. html'
- Config:
- Category: 1
- News_id: 2
- Defaults:
- Controller: News
- Action: Show
- # Parse the old news page url
- News_page_keep_old:
- RegEx: '/([0-9] +) \. html'
- Config:
- News_id: 1
- Defaults:
- Controller: News
- Action: Show
CopyCode
The preceding rules have a higher priority.
When the requested URL is a http://msplinks.com.cn/MDFodHRwOi8vbXkuY29tLzEyMzQ1Ni5odG1s, the previous rule match fails, and the subsequent rule match succeeds. Therefore, the request is sent to the show action method of the news controller for processing, and the news_id parameter is included.
- Function actionshow ()
- {
- $ News_id = intval ($ _ Get ['news _ id']);
- $ News = News: Find ($ news_id)-> get ();
- ... Display news content
- }
Copy code
This ensures the validity of the original URL.
Then when the request is a http://msplinks.com.cn/MDFodHRwOi8vbXkuY29tL25ld3Mvc3BvcnRzLzEyMzQ1Ni5odG1s, the previous rule will match successfully, then it will not match the subsequent rule. The request is still processed by actionshow. The preceding rule has an additional category parameter, which is not used here. Ignore it directly. However, this parameter is indispensable and will be used for subsequent URL generation.
Now let's take a look at the url generation code in the template:
- URL ('news/show', array (
- 'News _ id' => $ news-> ID,
- 'Category '=> $ news-> category_name
- ))
Copy code
We can see that the category parameter is used to generate a URL. Therefore, the preceding rule is used to generate a URL. Eventually we will get results like http://msplinks.com.cn/MDFodHRwOi8vbXkuY29tL25ld3MvY2Fycy8yMzM0My5odG1s.
Now we have solved the problem perfectly: it not only ensures the validity of the previous URL, but also makesAccessAfter accessing the old page, you can navigate to the new format URL through the URL in the page.
--------------------------------------
URL changes as needed
In addition to Seo, URL routing also allows us to construct URL addresses as we like. For example, the URL rule for http://msplinks.com.cn/MDFodHRwOi8vcWVlcGhwLmNvbS9jYXNlcy8xOQ== is:
- Cases_show:
- Pattern: "/cases/: ID"
- Defaults:
- Controller: "cases"
- Action: "show"
- Config:
- ID: "[0-9] +"
Copy code
We changed the rule:
- Cases_show:
- RegEx: '/mygod/show \-me \-the \-money/news \-sports \-(\ D +) \. html'
- Defaults:
- Controller: "cases"
- Action: "show"
- Config:
- ID: 1
Copy code
Then the URL becomes the http://msplinks.com.cn/MDFodHRwOi8vcWVlcGhwLmNvbS9teWdvZC9zaG93LW1lLXRoZS1tb25leS9uZXdzLXNwb3J0LTE5Lmh0bWw=. It can be said that it is similar to the previous URL.
These modifications and changes do not require us to modify the application.ProgramIn the code and template code, the only thing that needs to be modified is the rule table.