【问题标题】:wamp local subdomains and htaccess rewrite ruleswamp 本地子域和 htaccess 重写规则
【发布时间】:2013-02-04 17:05:45
【问题描述】:

我最近用 php mvc 魔法修复了我的网站,为此我在我的 .htaccess 中使用了这些设置:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]

它就像一个魅力。我遇到的唯一问题是,当我相对引用 css 或图像等文件时,由于 url 中的破折号,浏览器假定我们向下移动了文件夹结构 本地主机/项目名称/控制器名称/操作/id

所以在 localhost/controllername/action/id/css/styles.css 请求了 css/styles.css 绝对路径解决了这个问题,但在没有“项目名称”-文件夹后,本地测试服务器和远程实时和开发服务器无法正常工作

我决定配置我的本地 wamp 服务器,这样我就可以像“http://projectname.localhost”一样将我的不同项目作为子域访问到我的 localhost。

我激活了相关的 apache 模块,编辑了我的 hosts 文件并将我的 httpd-vhosts.conf 更改为

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName localhost.com
    ServerAlias www.localhost.com
    DocumentRoot "C:\wamp\www"
    ErrorLog "logs\errors.log"
    <directory "C:\wamp\www">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName localhost.com
    ServerAlias *.localhost.com
    VirtualDocumentRoot "C:\wamp\www\subdomains\%1"
    ErrorLog "logs\errors.log"
    <directory "C:\wamp\www\subdomains\%1">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

现在当我打电话给 projectname.localhost.com/controllername/action/id 它告诉我 在此服务器上未找到请求的 URL /subdomains/projectname/index.php。 虽然 projectname.localhost.com/index.php 仍然为我提供了正确的文件。

我听说 $_SERVER['DOCUMENT_ROOT']; 可能有问题。设置不正确,但知道这还没有帮助我解决这个问题。

有人能指出我正确的方向或建议其他环境配置,以便在这些情况下进行测试吗?

【问题讨论】:

    标签: php .htaccess mod-rewrite wamp mod-vhost-alias


    【解决方案1】:

    将我的 .htaccess 更改为

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
    

    解决了这个问题。 魔法线是RewriteBase /

    似乎服务器真的在错误的地方寻找 index.php。

    希望这可以帮助其他遇到同样问题的人... :)

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 2010-09-26
      • 2011-11-21
      • 2012-07-31
      • 1970-01-01
      • 2013-05-23
      • 2014-03-06
      • 2017-05-03
      • 1970-01-01
      相关资源
      最近更新 更多