2016-10-04 103 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