开发者

Redirect a controller to main index

I have a controller I am using for redirects called redirect.php. This controller has a single _remap($method).

This _remap method works by attributing any URI string after the redirect controller back into the controller. For example, a request to http://mysite.com/redirect/sometext would send the request back to http://mysite.com/redirect

This is the behavior I want, but how I would make this happen at the index level so instead of having to type http://mysite.com/redirect/sometext , I could type http://mysite.com/some开发者_运维知识库text and get the same behavior. This is challenging because Index.php is used by CodeIgniter so I can't put the _remap method here. As, I'm not super familiar with routes and .htaccess any help would be appreciated to get this working.


If you wanted to go the routes way, you would make a route for each controller you have, aka controller somepage would have a route $route["somepage"] = "somepage";, then after all of them (at the very bottom of the routes). Routes work by finding the first matching regex and then using it, so to make sure all of your controllers work, and only catch regular text, you need each of these rules.
Then we make the 'default' rule, this would run for everything that didn't get caught in a previous route check. Something like $route["(.*)"] = 'redirect/index/$1';. This would redirect you to the controller redirect, with the first uri segment (segment 3) being the sometext in your example above. Do note that this will also run for 404 pages, it would run for everything that isn't defined in the above rules.

I don't see a point in this, if I were you I would just send it to the redirect controller, it would make things a bit less messy, but there is of course a way as shown above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜