简体   繁体   English

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

[英]Redirect URL with .htaccess to Subdirectory

I am running a website and a CakePHP server on the same service.我在同一个服务上运行一个网站和一个 CakePHP 服务器。 The CakePHP is in a subdirectory. CakePHP 位于子目录中。 I would like the URL http://www.example.com to direct to my index.html in the root and http://www.example.com/app to redirect to CakePHP webroot.我希望 URL http://www.example.com指向我的根目录中的 index.html,并将http://www.example.com/app重定向到 CakePHP webroot。

These are my current .htaccess files这些是我当前的 .htaccess 文件

In var/www/html:在 var/www/html 中:

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

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

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

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

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

Edit编辑

The question is how would I set up my htaccess so that I can get to my CakePHP webroot with the URL http://www.example.com/app but on the index var/www/html if I use the URL http://www.example.com .问题是我将如何设置我的 htaccess 以便我可以使用 URL http://www.example.com/app但在索引 var/www/html 上访问我的 CakePHP webroot 如果我使用 URL http:/ /www.example.com With my current htaccess it redirects both URL to the CakePHP webroot.使用我当前的 htaccess,它将两个 URL 重定向到 CakePHP webroot。

Thank you谢谢

Do you have root access to apache config?你有 apache 配置的 root 访问权限吗?

Im guessing your cake app is not the only thing you want to run on this domain so you are wanting to set it up as an alias on /app ?我猜你的蛋糕应用不是你想在这个域上运行的唯一东西,所以你想把它设置为/app上的别名?

I recommend setting up /app as an Alias in apache config first.我建议先在 apache 配置中将/app设置为别名。

Im more familiar with doing this on Ubuntu 14.04 (my example is specific to ubuntu) but the concept will be the similar on other Linux distributions我更熟悉在 Ubuntu 14.04 上执行此操作(我的示例特定于 ubuntu),但在其他 Linux 发行版上的概念与此类似

Enable the alias mod:启用别名模式:

sudo a2enmod alias

using nano or other favourite text editor you need to add a file to apache config:使用 nano 或其他喜欢的文本编辑器,您需要将文件添加到 apache 配置:

sudo touch /etc/apache2/sites-available/mycakeapp.alias.conf
sudo nano /etc/apache2/sites-available/mycakeapp.alias.conf

For the file content:对于文件内容:

Alias /app "/var/www/html/app/webroot"
<Directory "var/www/html/app/webroot">
    Options Indexes FollowSymLinks
    Order allow,deny
    Allow from all
</Directory>

Enable the config file so apache knows to load it:启用配置文件,以便 apache 知道加载它:

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

Restart apache to load in the new config:重新启动 apache 以加载新配置:

sudo service apache2 restart

then rewrite the base in your .htaccess file in /app/webroot然后在/app/webroot中的.htaccess文件中重写基础

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

Done.完毕。 http://www.example.com/app will now point to and act as your applications base directory http://www.example.com/app现在将指向并充当您的应用程序基目录

the other .htacess files /.htaccess and /app/.htaccess can be removed as they will never be hit.其他 .htacess 文件/.htaccess/app/.htaccess可以删除,因为它们永远不会被命中。 Not compulsory to point the virtual host straight to the webroot but it is better practise as it will stop users being able to access or view other folders in the app directory将虚拟主机直接指向 webroot 不是强制性的,但更好的做法是因为它会阻止用户访问或查看应用程序目录中的其他文件夹

Centos / RedHat setting up apache alias Centos/RedHat 设置 apache 别名

Adding this to the bottom of your /etc/httpd/conf/httpd.conf file and restarting apache should do the trick.将此添加到/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>

The Short Answer to your question对您问题的简短回答

If you dont have root access to apache config or just want to skip the alias adding step you can just add the RewriteBase /app line to your webroots .htaccess如果您没有对 apache 配置的 root 访问权限或只想跳过别名添加步骤,您可以将RewriteBase /app行添加到您的 webroots .htaccess

Delete or edit the .htaccess in /var/www/html so that the myexample.com/ doesnt redirect to the cake app删除或编辑/var/www/html.htaccess以便myexample.com/不会重定向到 cake 应用程序

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

Your folders structure:您的文件夹结构:

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

Inside var/www/html add .htaccess file在 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