2016-10-04 100 views
0

我很難理解htaccess重寫/重定向是如何工作的。我已經有超過20篇關於它的文章,但我沒有理解如何正確編寫規則的邏輯和語法。在子文件夾下如何用htaccess重寫或重定向子目錄?

我目前沒有與的public_html文件夾 我們有我們的主網站的子文件夾/站點下的服務器,這是一個WordPress站點 我們還有其他的網站(也是WordPress站點),我們使用電子學習/培訓

我需要讓自己的網站(domain.com),同時具有domain.com/learning指向的public_html /培訓

我試圖understad htaccess的查詢如何爲指向/的public_html /網站工作如RewriteCond或RewriteRule

這裏是我當前的代碼:

# Use PHP5.4 Single php.ini as default 
AddHandler application/x-httpd-php54s .php 


# File modified on Sun Mar 30 01:22:23 2014 by server 
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives 
# php_value max_execution_time 90 

# BlueHost.com 
# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
# For security reasons, Option followsymlinks cannot be overridden. 
#Options +FollowSymLinks 
Options +SymLinksIfOwnerMatch 
RewriteEngine on 



#RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 

#RewriteCond %{REQUEST_URI} !^/site/ 


# 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 ^(.*)$ /site/$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.)?domain.com$ 
RewriteRule ^(/)?$ site_en/index.html [L] 


# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on 
# Change domain.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
# Change 'site to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/learning/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'site' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /learning/$1 
# Change domain.com to be your main domain again. 
# Change 'site' 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.)?domain.com$ 
RewriteRule ^(/)?$ learning/index.html [L] 

回答

1

下面的代碼將需要添加到您的託管帳戶的的public_html文件夾中的.htaccess文件。

您需要插入以下代碼塊並進行修改,如(#)註釋中所述。

# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on 
# Change domain.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
# Change 'site to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/site/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'site' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /site/$1 
# Change domain.com to be your main domain again. 
# Change 'site' 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.)?domain.com$ 
RewriteRule ^(/)?$ site/index.html [L] 

重複學習域相同。

遊客到您的網站將不能告訴你的主要領域是使用一個子目錄,他們仍然會看到網站地址爲

http://www.domain.com/index.html

注:

請注意,這不會與某些網站的軟件工作。您 可能還需要修改$ base_url,$ live_site或其他 配置設置以完成該過程。

既然你提到,你的情況是WordPress的,別忘了看看這個太

https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

更新:

RedirectMatch 301 /learning/(.*) /training/$1 

# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on 
# Change domain.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
# Change 'site to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/site/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'site' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /site/$1 
# Change domain.com to be your main domain again. 
# Change 'site' 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.)?domain.com$ 
RewriteRule ^(/)?$ site/index.php [L] 
+0

我說得有點快。我們的學習網站使用htpasswd進行保護。將這兩個代碼放在一起,使得主站點不能在沒有輸入密碼的情況下訪問。 – chris

+0

@chris,你爲什麼收回答案驗證隊友? –

+0

,因爲它不起作用。我用當前的htaccess發佈了最初的主題。 – chris