【问题标题】:Enable Mod_Reweite script (Google App Engine)启用 Mod_Rewrite 脚本 (Google App Engine)
【发布时间】:2015-12-31 18:24:10
【问题描述】:

如果之前已经回答过这个问题,我很抱歉,但我找不到答案。我有一个 .htaccess,RewriteEngine On 并且其中是规则:

  RewriteEngine on
  RewriteRule ^(.*) public/$1 [L]

我想在我的 app.yaml 的处理程序中调用的 mod_rewrite.php 中解释此规则

mod_rewrite.php:

 <?php
 /**
 * @file
 * Provide basic mod_rewrite like functionality.
 *
 * Pass through requests for root php files and forward all other requests to
 * index.php with $_GET['q'] equal to path. The following are examples that
 * demonstrate how a request using mod_rewrite.php will appear to a PHP script.
 *
 * - /install.php: install.php
 * - /update.php?op=info: update.php?op=info
 * - /foo/bar: index.php?q=/foo/bar
 * - /: index.php?q=/
 */

 $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

 // Provide mod_rewrite like functionality. If a php file in the root directory
 // is explicitly requested then load the file, otherwise load index.php and
 // set get variable 'q' to $_SERVER['REQUEST_URI'].
 if (dirname($path) == '/' && pathinfo($path, PATHINFO_EXTENSION) == 'php') {
 $file = pathinfo($path, PATHINFO_BASENAME);
 }
 else {
 $file = 'index.php';

 // Provide mod_rewrite like functionality by using the path which excludes
 // any other part of the request query (ie. ignores ?foo=bar).
 $_GET['q'] = $path;
 }

// Override the script name to simulate the behavior without mod_rewrite.php.
// Ensure that $_SERVER['SCRIPT_NAME'] always begins with a / to be consistent
// with HTTP request and the value that is normally provided.
$_SERVER['SCRIPT_NAME'] = '/' . $file;
require $file; 

【问题讨论】:

    标签: php regex .htaccess google-app-engine mod-rewrite


    【解决方案1】:

    我可能完全错了,但不会

    $path = 'public'.parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
    

    够了吗?

    另外,改变

    的字符串
    $file = 'index.php';
    

    $file = 'public/index.php';
    

    *如果你的 index.php 在公用文件夹中,则忽略最后一点。

    【讨论】:

    • 当我离开顶行时,我得到一个空白页。但是当我按照您的建议执行public/index.php 时,我收到错误Warning: require(../vendor/autoload.php): failed to open stream: 以及致命错误。这意味着它只找到该脚本但不调用查看所有其他脚本,例如autoload.php
    • 尝试将 mod_rewrite 拖入您的公用文件夹并在 .yaml 文件中将“script: mod_rewrite.php”更改为“script: public/mod_rewrite.php”
    • 非常棒,但仅适用于主页。它开始工作,但是当我点击导航时,所有其他 URL 都不起作用。
    • 点击其他链接时会发生什么?它会返回主页还是给出错误?链接指向的 URL 是什么?
    • 浏览器中的 url 发生变化,但页面保持不变。我使用 laravel 和 composer,我的目录结构是基于 MVC 的。例如视图在application/view/example_script.php,模态在application/modal/example_script.php,控制器在application/controller/example_script.php
    猜你喜欢
    • 2023-04-06
    • 2017-04-25
    • 2014-04-16
    • 1970-01-01
    • 2015-05-15
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多