.htaccess - 如何使用 .htaccess 将子域别名/重定向到另一个域子文件夹

标签 .htaccess mod-rewrite amazon-ec2 bitnami mod-alias

我希望我的用户访问

red.example.com

他们实际看到的是一个由

提供的页面

blue.example.com/red

使用.htaccess可以实现这一点吗?本质上它是一个重定向,但我不想在地址栏中更改 URL。

编辑

添加虚拟主机信息。

RewriteEngine On
RewriteRule /<none> / [L,R]

<IfDefine USE_PHP_FPM>
    <Proxy "unix:/opt/bitnami/php/var/run/wordpress.sock|fcgi://wordpress-fpm" timeout=300>
    </Proxy>
</IfDefine>

<Directory "/opt/bitnami/apps/wordpress/htdocs">
    AuthType Basic
    AuthName Coincurve
    AuthUserFile "/opt/bitnami/apache2/wordpress_users"
    Require valid-user

    Options +MultiViews +FollowSymLinks
    AllowOverride None
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        # Require all granted
    </IfVersion>

    <IfDefine USE_PHP_FPM>
       <FilesMatch \.php$>
         SetHandler "proxy:fcgi://wordpress-fpm"
       </FilesMatch>
    </IfDefine>

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]

    Include "/opt/bitnami/apps/wordpress/conf/banner.conf"
</Directory>

Include "/opt/bitnami/apps/wordpress/conf/htaccess.conf"

最佳答案

I've only got one actual server at mydomain.com. the red and blue subdomains are just both pointing to that mydomain.com's IP address.

如果子域都指向同一个地方(主域的根),那么您可能不需要“重定向”到不同的主机名(这将涉及使用 mod_proxy 并配置 反向代理)。换句话说,blue.example.com/red 指向与 red.example.com/red 相同的内容 - 因此它成为相对简单的内部重写.

(虽然您的虚拟主机配置不完整,但我假设您在某处有一些 ServerAlias 指令?)

例如:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^red\.example\.com [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ /red%{REQUEST_URI} [L]

REDIRECT_STATUS环境变量的检查是为了确保我们只重写来自客户端的初始请求,即。不重写请求,从而避免重写循环。 REDIRECT_STATUS 最初为空,第一次成功重写后设置为“200”。否则,这将无条件地重写所有内容,因此如果您愿意,可以在根 /red 目录中存在 /red 子目录。

关于.htaccess - 如何使用 .htaccess 将子域别名/重定向到另一个域子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151085/

相关文章:

amazon-ec2 - EC2 Windows 实例初始化两次

php - 为什么这个 .htaccess 代码会不断重定向?

.htaccess - 从 URL 中隐藏目录名称

amazon-web-services - AWS CodeDeploy 失败 : The deployment failed because a specified file already exists at this location

regex - 如何使用 htaccess 仅在 url 路径上显示 id 值?

regex - htaccess regexp 下划线和空格不起作用

amazon-ec2 - Amazon Web Service EC2 实例 RedHat yum 不起作用

php - 登录页面与 htpasswd - 哪个更安全?

regex - 在 .htaccess 中禁用 HTTP 跟踪

apache - 将动态页面和非 www 重写为 www (.htaccess)