【问题标题】:How to rewrite or redirect in subdirectory with htaccess?如何使用 htaccess 在子目录中重写或重定向?
【发布时间】:2016-10-04 07:57:43
【问题描述】:

我很难理解 htaccess 重写/重定向是如何工作的。我已经阅读了 20 多篇关于它的文章,但我没有掌握如何正确编写规则的逻辑和语法。

我目前有一个带有 public_html 文件夹的服务器 我们在子文件夹 /site 下有我们的主站点,这是一个 wordpress 站点 我们在子文件夹 /training 下还有其他用于电子学习的网站(也是 wordpress 网站)

我需要让我们的网站 (domain.com) 指向 /public_html/site,同时让 domain.com/learning 指向 public_html/training

我试图了解 htaccess 中的查询是如何工作的,例如 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] 

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    需要将以下代码添加到您主机帐户的 public_html 文件夹中的 .htaccess 文件中。

    您需要插入以下代码块并按照 (#) cmets 中的说明进行修改。

    # .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] 
    

    【讨论】:

    • 我说得有点快。我们的学习网站受 htpasswd 保护。将这两个代码一个接一个地放置,使主站点在不输入密码的情况下无法访问。
    • @chris,你为什么收回答案验证伙伴?
    • 因为它不起作用。我用我当前的 htaccess 发回了最初的主题。
    • 伙计,你有很多重写 htaccess,删除整个代码并粘贴我单独提供的代码,然后用你的域名修改它并再次检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2013-05-03
    • 2023-04-10
    • 2016-05-02
    相关资源
    最近更新 更多