【问题标题】:Apache's Alias directive is not working as expectedApache 的 Alias 指令未按预期工作
【发布时间】:2013-09-10 06:02:38
【问题描述】:

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

我的网站:

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 配置文件本身工作。请帮我做这件事。

【问题讨论】:

  • 可以把portal.com的配置贴在这里吗?
  • 完成。将其添加到问题本身。

标签: php apache2 rewrite codeigniter-2 mod-alias


【解决方案1】:

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 会达到重写规则,但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

【讨论】:

  • example.png的完整路径是什么?
  • 这里 - /home/example/portal/CodeIgniter_2.1.0/images/example.png
  • 你可以用http://portal.com/images/example.png访问这个图片,现在你想用http://example.com/portal/images/example.png访问这个文件吧?
  • 回复您的回答。你说“不会打”。但是下面的重写条件工作正常。为了验证它,我做了RewriteCond $1 ^(css|images|js) RewriteRule ^(.*)$ css,你看到example.com/portal/images 或js 将我重定向到example.com/portal/css
  • 是的,example.com/portal/images 会命中,但 example.com/images 不会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2018-02-27
  • 2016-03-31
  • 1970-01-01
相关资源
最近更新 更多