php - Apache 的别名指令未按预期工作

标签 php apache2 rewrite codeigniter-2 mod-alias

我正在尝试将我的门户与我的网站集成。

我的网站:

http://example.com

和我的门户:

http://portal.com

现在我想查看我的门户:

http://example.com/portal

我的核心 apache 配置文件的一部分(启用站点/网站):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    DocumentRoot /home/example/WebSite2.0/WebContent
    DirectoryIndex index.php index.html

    <Directory /home/example/WebSite2.0/WebContent>
            Options  +IncludesNOEXEC
            AllowOverride None
            Order allow,deny
            allow from all
            XBitHack On
            AddType text/html .html
            AddHandler server-parsed .html   
    </Directory>

    Alias /portal /home/example/portal/CodeIgniter_2.1.0
    <Directory /home/example/portal/CodeIgniter_2.1.0>
            DirectoryIndex "index.php"
            allow from all
            Options +Indexes
            #Options  FollowSymLinks MultiViews
            Order allow,deny

            RewriteEngine On
            RewriteBase /portal
            #RewriteRule ^test\.html$ test.php 

            RewriteCond $1 !^(index\.php|css|images|robots\.txt)
            RewriteRule ^(.*)$ index.php/$1 [L]

            RewriteCond $1 ^(css|images|js)
            RewriteRule ^(.*)$ $1

    </Directory>
</VirtualHost>

如您所见,我的门户功能在 CodeIgniter 之上;因此——

 RewriteCond $1 !^(index\.php|css|images|robots\.txt)
 RewriteRule ^(.*)$ index.php/$1 [L]

我的核心 apache 配置文件的一部分(启用站点/门户):

<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName portal.com
    ServerAlias www.portal.com
    DocumentRoot /home/example/portal/CodeIgniter_2.1.0
    DirectoryIndex "index.php"
    SSLEngine On
    SSLCertificateFile "ssl/portal.com.crt"
    SSLCertificateKeyFile "ssl/portal.com.key"
    <Directory /home/example/portal/CodeIgniter_2.1.0>
            Options  FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Header unset Server
            ServerSignature Off
    </Directory>
</VirtualHost>

现在真正的问题是当我打开 http://example.com/portal 时浏览器正在 DocumentRoot 而不是 Alias 中寻找图像。

例如图片来自门户,

<img src="/images/example.png" style="margin-left:30px;height:50px;">

apache 错误日志说 -

File does not exist: /home/example/WebSite2.0/WebContent/images/example.png

我不愿意更改我的代码。我只想让这个东西从 apache 配置文件本身开始工作。请帮我做这件事。

最佳答案

RewriteBase/portal要求url以/portal开头。所以:

RewriteCond $1 !^(index\.php|css|images|robots\.txt)

不会被击中。

<img src="/images/example.png" style="margin-left:30px;height:50px;">

将尝试从 DocumentRoot 搜索文件。

更新1

因为有RewriteBase/portalexample.com/portal/images会命中Rewrite规则,但是example.com/images不会,所以:

 <img src="/images/example.png" style="margin-left:30px;height:50px;">

应该是:

 <img src="/portal/images/example.png" style="margin-left:30px;height:50px;">

更新2

这是@Hussain Tamboli 自己给出的答案,有:

RewriteRule /(images|js|css)/(.+)\.(.+)$ /portal/$1/$2.$3 [PT].

/images/Invoice.png 将重写为 /portal/images/Invoice.png

关于php - Apache 的别名指令未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711695/

相关文章:

php - 在没有 shell 的情况下将 HTML 转换为图像

apache2 - 如何通过名称和 IP 访问 Apache 虚拟主机

regex - 当包含 '?' 时,如何在 Charles Proxy 中设置重写规则?

php - 我如何使用 mod-rewrite (iirf) 只重定向 php 文件

php - 在这个阶段从 PHP/MySQL 切换到 .NET 是否值得?

php - 从 PHP 数组获取值并显示它们

php - 无法与 PayPal 网关通信

.htaccess - 拒绝使用 .htaccess 直接访问文件或目录

php - ftp_get 损坏音频文件

wordpress - 允许多个 IP 通过 .htaccess 访问 Wordpress Site Admin