将所有请求重定向到其他位置的根目录?

时间:2012-08-03 16:15:17

标签: php apache .htaccess

好吧,所以我已经为我正在设置的服务器下载了一个CMS,我对这些文件的路径有些困难。

我已将CMS放在我服务器上的子目录中,因为根目录已被另一个CMS使用。但是,CMS在代码中重复使用“/”链接到其文件。例如,使用以下代码找到图像:

<img src="/images/banner.png" />

如您所知,这不起作用,因为上面的链接会将请求重定向到服务器根目录中的images文件夹,而不是子目录。 CMS还附带了一个“.htaccess”文件,因此我立即想到我可以将使用“/”字符发出的所有请求重定向到服务器上的子目录。这是原始的“.htaccess”文件:

RewriteEngine On

RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1

RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php

RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php

RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1

ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php

我想通过添加

RewriteRule ^/$ /subdirectory/

对“/”的请求将被重定向,但无济于事。我已经检查了apache配置,似乎已经启用了使用htaccess文件覆盖配置,所以如果有人能够帮助我,我会非常高兴。

提前致谢!

修改 我真正接近解决方案。我在“RewriteEngine on”下面插入了这段代码:

RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2

访问网址“http://mysite.com/private_servers/strebbohotel/content/.php”时返回以下错误页面: 找不到

在此服务器上找不到请求的网址/private_servers/strebbohotel/.php。

此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。

如您所见,它出于某种原因跳过了“content”子目录。我相信它与.htaccess文件的其他行有关,但我无法弄清楚哪些。也可能是我需要一个更复杂的正则表达式,但我并不是特别擅长,所以如果有人能帮助我,我会感激不尽。

3 个答案:

答案 0 :(得分:1)

CMS子目录中的.htaccess文件对对服务器根目录发出的请求没有影响。你需要在那里添加.htaccess。

即使这样,我也不知道如何将来自CMS的请求(例如示例中的图像请求)与用于Web根目录的应用程序区分开来。

您的CMS是否没有任何配置设置允许您指定除Web根目录以外的文件的路径?

答案 1 :(得分:1)

您必须更改根目录上的.htaccess文件 - 这会弄乱其他CMS。我想到的唯一解决方案是使用RewriteCond的组合:

RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}

这应该可以解决问题。如果要自定义它,请参阅this cheatsheet(或simmilar)。但请注意,根目录中的.htaccess将根据对您的子目录cms发出的所有请求进行检查 - 因此需要第二个条件。

答案 2 :(得分:1)

您想使用:

RewriteBase /another_root_dir/

在.htaccess文件的开头

相关问题