【问题标题】:CakePHP without URL rewriting: redirect / to index.php没有 URL 重写的 CakePHP:将 / 重定向到 index.php
【发布时间】:2014-09-16 23:16:53
【问题描述】:

我在共享主机上使用 CakePHP 框架,无法打开 URL rewriting。 安装位于名为/tc 的文件夹中。当我打开 /tc/index.php/Controller/Action 时一切正常。

但是,我想让我的用户更轻松一点,这样他们就不必打开 /tc/index.php 而只需打开 /tc(然后应该重定向到 /tc/index.php)。

如何使用.htaccess 做到这一点?

谢谢!

澄清 我希望 /tc/ 上的请求被重定向到 /tc/index.php

解决方案

用一个简单的方法为我工作

RewriteEngine On
RewriteRule ^$ index.php/$1 [L,QSA]

CakePHP 处理其余部分,因为所有链接都指向 /tc/index.php/Controller。

感谢您的帮助!

【问题讨论】:

  • 你为什么不联系他们打开它或者找一个普通的共享主机?
  • 它确实有效,但是当我从外部站点通过 https 调用控制器时,由于一些问题,我无法使用 URL 重写。这仅在 URL 重写关闭时有效 =)

标签: php .htaccess cakephp rewrite


【解决方案1】:

尝试在.htaccess中使用以下代码

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
   IndexIgnore *
</IfModule>

【讨论】:

    【解决方案2】:

    试试这个:

    1) 从 apache 服务器打开“mod_rewrite”。

    2) 注释“Configure::write('App.baseUrl', env('SCRIPT_NAME'));”这行来自/confige/core.php

    【讨论】:

    • 这是一个共享主机,我无权做 apache conf。正如我所说,我不能使用 URL 重写。
    【解决方案3】:

    以下是 .htaccess 的规则,您可以通过该规则从 URL 中删除 index.php,这意味着如果有人输入 http://example.com/index.php,它将重定向到 http://example.com/。

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*index.php HTTP/
    RewriteRule ^(([^/]+/)*)index.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]
    

    以上是您可以将 index.php 重定向到根目录的两个规则。

    【讨论】:

    • 谢谢。但我想反过来:如果 /tc 被调用,重定向到 /tc/index.php
    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多