Php 使用.htaccess将URL重定向到子目录
我在同一个服务上运行一个网站和一个CakePHP服务器。CakePHP位于子目录中。我希望URL指向根目录中的index.html,并重定向到CakePHP webroot 这些是我当前的.htaccess文件 在var/www/html中:Php 使用.htaccess将URL重定向到子目录,php,.htaccess,cakephp,redirect,Php,.htaccess,Cakephp,Redirect,我在同一个服务上运行一个网站和一个CakePHP服务器。CakePHP位于子目录中。我希望URL指向根目录中的index.html,并重定向到CakePHP webroot 这些是我当前的.htaccess文件 在var/www/html中: <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
重新启动发动机
重写规则^$app/webroot/[L]
重写规则(*)app/webroot/$1[L]
在var/www/html/app中:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ /webroot/ [L]
RewriteRule (.*) /webroot/$1 [L]
</IfModule>
重新启动发动机
重写规则^$/webroot/[L]
重写规则(.*)/webroot/$1[L]
在var/www/html/app/webroot中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]
编辑
问题是如何设置htaccess,以便使用URL访问CakePHP webroot,但如果使用URL,则访问索引var/www/html。使用我当前的htaccess,它将两个URL重定向到CakePHP webroot
谢谢您有apache配置的root访问权限吗 我猜你的cake应用程序不是你想在这个域上运行的唯一东西,所以你想在
/app
上将它设置为别名
我建议首先在apache配置中将/app
设置为别名
我更熟悉在Ubuntu 14.04上做这件事(我的例子是针对Ubuntu的),但在其他Linux发行版上,这个概念将是类似的
启用别名模块:
sudo a2enmod alias
使用nano或其他常用的文本编辑器,您需要向apache配置添加一个文件:
sudo touch /etc/apache2/sites-available/mycakeapp.alias.conf
sudo nano /etc/apache2/sites-available/mycakeapp.alias.conf
对于文件内容:
Alias /app "/var/www/html/app/webroot"
<Directory "var/www/html/app/webroot">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
重新启动apache以在新配置中加载:
sudo service apache2 restart
然后在/app/webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
您问题的简短答案
如果您没有apache config的root访问权限,或者只想跳过别名添加步骤,您可以将RewriteBase/app
行添加到webroot.htaccess
删除或编辑/var/www/html
中的.htaccess
,以便myexample.com/
不会重定向到cake应用程序回答:
您的文件夹结构:
/var/www/html/
.htaccess <----- edit
index.html
app/ <------ cakephp
bin/
config/
src/
...
/var/www/html/
.htaccess我有点困惑,你的问题是什么?对不起,不清楚。问题是如何设置htaccess,以便使用URL访问CakePHP webroot,但如果使用URL,则访问索引var/www/html。使用我当前的htaccess,它将两个URL重定向到CakePHP webroot。您的CakePHP根在哪个目录中?CakePHP位于/var/www/html上的应用程序文件夹中。这里还有一个index.html。您描述这一点的方式非常混乱-不清楚(是的,我读过这个问题)您是否将应用程序放在域根(/var/www/html
)或子文件夹中(例如,/var/www/html/app/app/webroot
存在)。“应用程序”出现在url中是不正常的。还不清楚您使用的是什么版本的CakePHP,因为它与3.x不匹配,我可以猜您使用的是2.x,但我不需要猜。请澄清您的问题。我尝试了链接中给出的步骤并更改了var/www/html文件,但仍然出现404错误:在此服务器上找不到请求的URL/webroot/。他们似乎更可能没有使用3.x-在这种情况下-这不是好的建议(如果是这样的话)建议编辑root.htaccess文件谢谢您的回答,我的CakePHP设置似乎有问题。我通过重新安装CakePHP并删除/var/www/html中的.htaccess使其正常工作。我尝试了简短的回答,但得到了404响应:在此服务器上找不到请求的URL/webroot/。我使用的是AmazonEC2Linux发行版。我有根访问权限,但我找不到apache2文件夹。我真的认为在你的情况下,将你的应用程序设置为别名是一种方法。在谷歌快速搜索之后,你对Linux的喜好似乎与redhat/centos相似。。。请查看我刚刚添加的Centos/RedHat设置apache别名部分,看看是否有帮助谢谢您的回答,我的CakePHP设置似乎有问题。我通过重新安装CakePHP并删除/var/www/html中的.htaccess来实现它。
Alias /app "/var/www/html/app/webroot" #make sure this is actually your applications root
<Directory "var/www/html/app/webroot">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
/var/www/html/
.htaccess <----- edit
index.html
app/ <------ cakephp
bin/
config/
src/
...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^app$ app/ [L]
RewriteRule ^app/(.*) app/$1 [L]
</IfModule>