繁体   English   中英

使用 .htaccess 将 URL 重定向到子目录

[英]Redirect URL with .htaccess to Subdirectory

我在同一个服务上运行一个网站和一个 CakePHP 服务器。 CakePHP 位于子目录中。 我希望 URL http://www.example.com指向我的根目录中的 index.html,并将http://www.example.com/app重定向到 CakePHP webroot。

这些是我当前的 .htaccess 文件

在 var/www/html 中:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$    app/webroot/  [L]
    RewriteRule  (.*)  app/webroot/$1  [L]
</IfModule>

在 var/www/html/app 中:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ /webroot/ [L]
    RewriteRule (.*) /webroot/$1 [L]
</IfModule>

在 var/www/html/app/webroot 中:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

编辑

问题是我将如何设置我的 htaccess 以便我可以使用 URL http://www.example.com/app但在索引 var/www/html 上访问我的 CakePHP webroot 如果我使用 URL http:/ /www.example.com 使用我当前的 htaccess,它将两个 URL 重定向到 CakePHP webroot。

谢谢

你有 apache 配置的 root 访问权限吗?

我猜你的蛋糕应用不是你想在这个域上运行的唯一东西,所以你想把它设置为/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 知道加载它:

cd /etc/apache2/sites-available
sudo a2ensite mycakeapp.alias.conf

重新启动 apache 以加载新配置:

sudo service apache2 restart

然后在/app/webroot中的.htaccess文件中重写基础

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

完毕。 http://www.example.com/app现在将指向并充当您的应用程序基目录

其他 .htacess 文件/.htaccess/app/.htaccess可以删除,因为它们永远不会被命中。 将虚拟主机直接指向 webroot 不是强制性的,但更好的做法是因为它会阻止用户访问或查看应用程序目录中的其他文件夹

Centos/RedHat 设置 apache 别名

将此添加到/etc/httpd/conf/httpd.conf文件的底部并重新启动 apache 应该可以解决问题。 service httpd restart

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>

对您问题的简短回答

如果您没有对 apache 配置的 root 访问权限或只想跳过别名添加步骤,您可以将RewriteBase /app行添加到您的 webroots .htaccess

删除或编辑/var/www/html.htaccess以便myexample.com/不会重定向到 cake 应用程序

答案: https : //stackoverflow.com/a/33018144/1127933

您的文件夹结构:

/var/www/html/
  .htaccess <----- edit
  index.html
  app/ <------ cakephp
    bin/
    config/
    src/
    ...

在 var/www/html 中添加 .htaccess 文件

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^app$    app/    [L]
    RewriteRule    ^app/(.*) app/$1    [L]
</IfModule>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM