HtAccess - 将请求移动到新子域所需的Mod_Rewrite规则

HtAccess  - 将请求移动到新子域所需的Mod_Rewrite规则

问题描述:

I have a directory structure in my Php mvc app that is currently like this

/site.com
    .htaccess
    index.php
    /models
    /views..
    /controllers..
    /resources..
        /images.. 
            /misc..
                logo.png
            /products..
                product_image1.jpg
        /js..
        /css..

I want to move the resources folder to its own subdomain so the structure would resemble this

/site.com
    .htaccess
    index.php
    /models
    /views..
    /controllers..

/resources.site.com
    /images.. 
        /misc..
            logo.png
        /products..
             product_image1.jpg
        /js..
        /css..    

I need to know the rewrite rule that redirect requests for anything in the resources folder to the new subdomain instead.

I have tried this rule and a few others but it isn't working.

RewriteEngine On

RewriteBase /resources
RewriteCond %{HTTP_HOST} ^resources$ [NC]
RewriteRule ^(.*)$ http://localhost/resources.site.com/$1 [L]

Any help would be much appreciated. Thanks

我的Php mvc应用程序中有一个目录结构,目前是这样的 p> /site.com .htaccess index.php / models /views.. /controllers.. /resources.. / images .. / misc .. logo.png /products.. product_image1.jpg /js.. /css.. nn

我想要 将resources文件夹移动到其自己的子域,以便该结构类似于 p>

  /site.com 
 .htaccess 
 index.php 
 / models 
 / views。  。
 /controllers..
nn/resources.site.com 
 / images .. 
 /misc..
 logo.png 
 /products..
 product_image1.jpg 
 / js。  。
 / css .. 
  code>  pre> 
 
 

我需要知道将资源文件夹中任何内容的请求重定向到新子域的重写规则。 p>

我已尝试过这条规则和其他一些规则,但事实并非如此 工作。 p>

  RewriteEngine On 
 
RewriteBase / resources 
RewriteCond%{HTTP_HOST} ^ resources $ [NC] 
RewriteRule ^(。*)$ http:// localhost /  resources.site.com/$1 [L] 
  code>  pre> 
 
 

非常感谢任何帮助。 谢谢 p> div>

The rule you have is a little mixed up. If you want to rewrite requests for the resources directory to a new domain, you don't want to make one of the conditions a request for the new domain. Try something like this in the htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^resources\.site\.com$ [NC]
RewriteRule ^resources/(.*)$ http://resources.site.com/$1 [R=301,L]

This will make it so when you request http://site.com/resources/images/misc/logo.png, you will get redirected to http://resources.site.com/images/misc/logo.png