【问题标题】:Adding mod-rewrite to an existing PHP website将 mod-rewrite 添加到现有 PHP 网站
【发布时间】:2010-09-24 10:54:41
【问题描述】:

我正在更新一个 php 应用程序,该应用程序目前不使用 url 重写。目的是隐藏文件扩展名。网站的大体结构如下

root/
  index.php
  login.php
  page1.php
  page2.php
  page3.php
  page4.php
  page5.php
  page6.php
  page7.php
  page8.php
subfolder/
  index.php
  validpage.php
images/
css/

上述结构中的子文件夹是唯一的,它是唯一包含php文件的子文件夹。所有其他文件都在根文件夹中。

Mod Rewrite and PHPMod-Rewrite or PHP router?mod_rewrite, php and the .htaccess file这些问题我都经历过

更新 htaccess。感谢@thomasmalt

RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l 
RewriteRule ^.*$ - [NC,L]


# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# add .php to the url     
RewriteRule ^(.*)$ $1.php

现在的问题是,当我尝试访问不存在的页面(如 localhost/inex.php)时,我得到的是 500 而不是 404 错误。 Apache 错误日志 给了我以下错误。 (我访问了 localhost/index ,它加载了 index.php 的内容,然后我加载了 localhost/inex ,这给了我一个 500 错误。)

[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

更新:找到了我的问题here 的答案。问题解决了。

【问题讨论】:

  • 很难从帖子中了解您想要实现的目标,而且太长了。您应该考虑重写它以澄清。
  • @thomasmalt:我重写了重写问题

标签: php .htaccess mod-rewrite


【解决方案1】:

Apache 有一个选项:Options +MultiViews

如果启用此功能,则无需在 url 中放置文件扩展名。

【讨论】:

【解决方案2】:

我不完全确定你想要达到什么目的,我怀疑,没有不尊重的意思,你需要了解 mod_rewrite,以及 webserver 和 php 解析和执行文件的方式。如果不超过或低于您的理解水平,我不确定如何回答,但我会尝试。

我可以告诉你当前 .htaccess 正在做什么:(伪代码)

if REQUEST_FILENAME does not exist then
    rewrite request to /index.php/<the request>

因为 index.php 是您执行的 php 脚本,并且 $1 中的所有内容都将被忽略。 这就是为什么 index.php 无论你做什么都会被执行。

您可以尝试在 .htaccess 中添加以下内容:

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)$ $1.php

这应该接受请求并尝试将 .php 附加到它的末尾。不幸的是,我没有测试过,但你应该试一试。结果应该是这样的:

if request is "/news/article" 
    translate it to "/news/article.php"

你应该在一个月内每天阅读两次 mod_rewrite 文档,并购买 O'Reilly 的关于正则表达式的书。

如果你有开发服务器的管理员权限,你应该调查一下

RewriteLogLevel
RewriteLog

【讨论】:

  • 感谢@thomasmalt。我很感激。我试过阅读httpd.apache.org/docs/2.0/mod/mod_rewrite.html,但还没有完全理解它。 :) 并感谢伪代码。喜欢它。
  • 这条规则就像一个魅力。但是它不能很好地处理文件夹名称。如果我尝试访问 localhost/ 或 localhost/subfolder/ 我会收到 500 错误。我可以为此做任何事情。
  • @abel 是的。您可以使用当前设置做的最简单的事情是将 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l RewriteRule ^.*$ - [NC,L] 在你的 mod_rewrite 配置开始时(每个 cond 和 rule 在自己的行)
  • 它告诉 mod_rewrite 传递(而不是更改)文件 (-f) 目录 (-d) 或具有有效目标 (-l) 的符号链接存在的任何请求。
  • 运行良好。我已经更新了 .htaccess 文件。但是有一个问题。我在尝试访问不存在的文件(如 index.phpa)时收到 500 错误而不是 404 错误。见原帖。
【解决方案3】:

根据经验,将 mod_rewrite 改造到现有网站中比听起来更难。

这并不是因为 mod_rewrite 很难——实际上设置它并让您的新 URL 工作相对容易。这很难的原因是因为您现有的站点将所有旧 URL 硬编码到其中,因此您必须通过代码将它们全部更改为指向新的 URL 格式 - 如果您的代码这可能特别棘手动态构建 URL 字符串。您需要进行大量测试,以确保您已经掌握了所有可能的变化并且它们仍然有效。

另外值得一提的是,搜索引擎和外部网站将使用旧 URL 将历史链接指向您的网站,因此您无法完全摆脱它们。

我知道这不能回答您的问题,但我相信这些是您开始项目之前需要考虑的重要事项,因为对于现有网站,它并不是某些人认为的快速修复。

【讨论】:

    【解决方案4】:
    1. 它会将您发送到 root,因为您有 RewriteBase / 定义默认基... 如果您想留在子文件夹中,只需在子文件夹中使用 RewriteBase /subfolder/ 创建另一个 .htaccess 或删除它
    2. 您可以根据需要设置条件

      RewriteRule ^(([0-9]+)-)?page(-([0-9]+))?/?$    index.php?page=$2&id=$4 [L]
      

    这会将您从www.abcd.com/2-page-3 带到www.abcd.com/index.php?page=2&amp;id=3

    1. index.php 中的代码不需要包含任何内容。

    【讨论】:

    • 感谢您的回复。我在stackoverflow上的其他类似问题中读到了一些内容,其中说最好让php处理重定向,同时将每个页面重定向到index.php。我已经更新了问题。
    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多