簡體   English   中英

使用 htaccess 將根重定向到子文件夾而不在 URL 中顯示它

[英]Use htaccess to redirect root to subfolder without showing it in URL

我目前正在共享服務器上使用Silex 框架托管一個網站,但我遇到了問題...

Silex 就像 Symfony,在 /web/ 子文件夾中有一個 app.php 文件:該網站只能通過 URL website.com/web/ 訪問。 我無法創建虛擬主機,因為它是共享服務器,所以我認為解決方案是使用 .htaccess 文件...

我設法自動將 website.com 重定向到 website.com/web/,但我真的不喜歡這個選項。 我寧願 website.com 直接指向 website.com/web/ 但我不知道如何通過使用 .htaccess 文件來做到這一點。 我已經嘗試解決這個問題好幾個小時了,這讓我很傷心......

目前我使用這個文件:

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

但它只是將您從 website.com 重定向到 website.com/web

無論如何,我可以使用 .htaccess 文件使根 url 直接指向 /web 文件夾嗎?

非常感謝:)

如果你想獲得http://example.com/subdirectory只需鍵入http://example.com這應該工作。

# .htaccess main domain to subdirectory redirect 

RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /subdirectory/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subdirectory/ [L]

試試這個 htaccess 配置:

DirectoryIndex web/app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/web/
    RewriteRule (.*) /web/$1
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /web/app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM