简体   繁体   English

路由在localhost上有效,但在实时服务器上无效

[英]Routing works on localhost, but not on live server

When I access my site on MAMP like so, it works great 当我像这样在MAMP上访问我的网站时,效果很好

localhost/site/about-us/ 本地主机/站点/关于我们/

When I upload it to my remote server, and access it like this 当我将其上传到我的远程服务器并像这样访问它时

http://www.server.com/site/about-us/ http://www.server.com/site/about-us/

all requests go back to the 'default' set up in bootstrap.php . 所有请求都返回bootstrap.php设置的“默认”。

Here is my route setting. 这是我的路线设置。

Route::set('default', '(<page>)')
    ->defaults(array(
        'page' => 'home',
        'controller' => 'page',
        'action'     => 'index',
    ));

The problem is, whenever it gets uploaded to my server, any request like /about-us/ is always defaulting to home as specified when setting the route. 问题是,每当将其上传到我的服务器时,/ about-us /之类的任何请求始终默认设置为设置路由时指定的home If I change that default to 'about-us', every page goes to 'about us'. 如果我将默认值更改为“关于我们”,则每个页面都会转到“关于我们”。

Does anyone know what may be causing this? 有谁知道这可能是什么原因? Thanks 谢谢

UPDATE 更新

Here is a hack that works , but is sure ugly as hell. 这是一个可行的黑客,但肯定是地狱丑陋。 Still I'd prefer to know why it doesn't work as per expected. 我还是更想知道为什么它不能按预期工作。

// Hack because I can not get it to go to anything except 'default' below...

 $uri = $_SERVER['REQUEST_URI'];

 $uri = str_replace(url::base(), '', $uri);

 $page = trim($uri, '/');

 if ( ! $page) $page = 'home';


Route::set('default', '(<page>)')
    ->defaults(array(
        'page' => $page,
        'controller' => 'page',
        'action'     => 'index',
    ));

Your code is basically a catch all route (it's being matched for all requests). 您的代码基本上是一条通吃的路线(它与所有请求都匹配)。 You should restrict it like so. 您应该这样限制它。

Route::set('static', '(<page>)', array('page' => 'about-us'))
->defaults(array(
    'controller' => 'page',
    'action'     => 'index',
));

The 3rd parameter is a regular expression which defines what the route should match. 第三个参数是一个正则表达式,用于定义路由应匹配的内容。

That route will route everything matched in the regular expression to the page controller and its index action. 该路由会将正则表达式中匹配的所有内容路由到页面控制器及其索引操作。

You can then use $page = $this->request->param('page'); 然后,您可以使用$page = $this->request->param('page'); in your action. 在你的行动中。

Are you not mistaking $page for $action? 您是否将$ page误认为$ action?

If I try this, it works just fine. 如果我尝试这样做,它就可以正常工作。 Here's my controllers action method: 这是我的控制器操作方法:

public function action_index($page = NULL)

{
    var_dump($page);
}

If I browse to 如果我浏览到

localhost/site/blup

I see see a nice 我看到一个不错的

string(4) "blup"

being echo'd. 被回应。 I have the default route setup identical to yours. 我有与您相同的默认路由设置。

It sounds like Kohana's auto-detection of the URL isn't working for your server setup... What web server is it failing on? 听起来Kohana对URL的自动检测不适用于您的服务器设置...它在哪个Web服务器上出现故障?

You could alter the Request::instance()->execute()... line in the bootstrap to start with: 您可以在引导程序中更改Request :: instance()-> execute()...行以以下内容开头:

Request::instance($_SERVER['REQUEST_URI'])->execute()... 请求:: instance($ _ SERVER ['REQUEST_URI'])-> execute()...

That will ensure it uses the correct URI.. 这将确保它使用正确的URI。

That being said ... as The Pixel Developer says, your route looks.. odd .. to me ;) 话虽如此...正如Pixel Developer所说,您的路线对我来说很奇怪。

But - since it works on MAMP - The route is likely not the issue. 但是-由于它适用于MAMP-路由可能不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 phpmailer可在localhost上运行,但不能在实时服务器上运行 - phpmailer works on localhost but not on live server PHP套接字服务器在本地主机上工作,但在实时服务器上不工作 - php socket server works on localhost but not on live server AJAX 适用于本地主机,但不适用于实时服务器 - AJAX works on localhost, but doesn't on live server php: @fopen 在 localhost 中工作,但在实时服务器中无效 - php: @fopen works in localhost but not in live server Coinbase OAuth可以在localhost上运行,但不能在实时服务器上运行 - Coinbase OAuth works on localhost but not on live server Ajax调用可在实时服务器上运行,但不适用于本地主机 - Ajax call works on live server, but not in localhost CodeIgniter POST可在localhost上运行,但不能在实时服务器上运行 - CodeIgniter POST works in localhost but not on live server 实际上,API在LocalHost上有效,但在实时服务器上无效 - Indeed API works on LocalHost but not on live server Magento模块在localhost上运行良好,但在实时服务器上运行不正常 - Magento module works well on localhost but not on live server php套接字服务器仅在localhost上有效,而在实时服务器上不可用 - php socket server works only on localhost but not live server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM