[英]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.