在 SO 中尝试了很多示例,但似乎都不起作用。考虑我的设置:
/public_html/
/app/
/cgi-bin/
/lib/
/plugins/
/revamp/
/vendors/
我目前有一个 cakephp 网站 site.com
,其文件位于 /app/
文件夹下,我现在使用的 .htaccess 如下所示:
<IfModule mod_rewrite.c>
RewriteEngine on
ReWriteRule ^$ app/webroot/ [L]
ReWriteRule (.*) app/webroot/$1 [L]
</IfModule>
我想将子域 revamp.site.com
重定向到文件夹 /revamp/
,这将是另一个 cakephp 站点。
更新 1:
被要求创建一个虚拟主机,我做到了,它被设置到子域文件夹,我想要的帮助是一个 htaccess 规则对两者都有效,上面的那个,如果请求的地址有子域,也重定向到子域在域之前...
更新 2:
下面的 .htaccess
让我进入我的子域,但仅限于 index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com$
RewriteRule (.*) app/webroot/$1 [L]
RewriteCond %{HTTP_HOST} ^revamp\.site\.com$
RewriteRule (.*) revamp/index.html [L]
</IfModule>
它是两个答案的混合体……尽管我为子域放置的任何路径,它只会让我进入 index.html(因为它在 htaccess 中被硬编码)。还尝试了 revamp/$
和 revamp/$1
但没有成功。
更新 3:
尝试编辑第一个答案:
<IfModule mod_rewrite.c>
RewriteEngine on
#subdomain simple site
RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$
RewriteCond %{REQUEST_URI} !^/subdomain
RewriteRule ^(.*)$ subdomain/$1 [L]
#cakephp site
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
还是给了500,我也试过换两个方 block 的位置
更新 4:
工作解决方案,在 /public_html/app
和 /public_html/revamp/app
下都有两个 cakephp 站点
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^revamp\.site\.com$
RewriteRule ^(.*)$ revamp/app/webroot/$1 [NC,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [NC,L]
</IfModule>
最佳答案
您可以通过针对不同的域使用“重写条件”来实现所需的重写:
在我的机器上,我创建了一个文件夹结构,如下所示
public_html
-app
-webroot
-local
-app
-webroot
public_html
文件夹中的 app、webroot 和本地目录,然后是本地文件夹中的 app 和 webroot 目录。
现在我创建了一个虚拟主机条目如下:
<VirtualHost test.com:80>
ServerName test.com
ServerAlias local.test.com
DocumentRoot /var/www/html/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
表示 test.com
和 local.test.com
都指向 public_html
目录。
现在你的目标是,如果域是 test.com
那么外部应用程序应该运行,如果域是 local.test.com
那么你的内部(本地文件夹)应用程序应该运行。
作为cakephp 中.htaccess
的结构,我将.htaccess
文件放在app
和webroot
文件夹中两种应用。
app/
目录下的.htaccess
代码
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
.htaccess
的代码在webroot/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
现在更改根目录的 .htaccess
即 public_html
:
<IfModule mod_rewrite.c>
RewriteEngine on
#for test.com
RewriteCond %{HTTP_HOST} ^test\.com$
RewriteRule ^(.*)$ app/webroot/$1 [NC,L]
#for local.test.com
RewriteCond %{HTTP_HOST} ^local\.test\.com$
RewriteRule ^(.*)$ local/app/webroot/$1 [NC,L]
</IfModule>
- RewriteCond
%{HTTP_HOST} ^test\.com$
将识别域是test.com
- RewriteCond
%{HTTP_HOST} ^local\.test\.com$
将识别 域是local.test.com
对于一般的子域重写,您可以使用以下代码:
文件夹结构:
public_html
-index.html
-subdomain
--index.html
.htaccess
的代码
<IfModule mod_rewrite.c>
RewriteEngine on
#for subdomain.test.com
RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$
RewriteCond %{REQUEST_URI} !^/subdomain
RewriteRule ^(.*)$ subdomain/$1 [L]
</IfModule>
现在 test.com 将打开 public_html
文件夹的 index.html
而 subdomain.test.com
将打开 index.html
子域
文件夹的
更新答案 2
我创建了问题中提到的确切目录结构,下面的 htaccess 代码适用于我:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.com$
RewriteRule ^(.*)$ app/webroot/$1 [NC,L]
RewriteCond %{HTTP_HOST} ^revamp\.test\.com$
RewriteCond %{REQUEST_URI} !^/revamp
RewriteRule ^(.*)$ revamp/$1 [NC,L]
</IfModule>
关于php - 将子域重定向到包含 CakePHP 应用程序的主目录中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181687/