Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 限制;“静态”;子域别名到非HTML文件,否则重定向到;www";_Apache_.htaccess_Mod Rewrite_Apache2 - Fatal编程技术网

Apache 限制;“静态”;子域别名到非HTML文件,否则重定向到;www";

Apache 限制;“静态”;子域别名到非HTML文件,否则重定向到;www";,apache,.htaccess,mod-rewrite,apache2,Apache,.htaccess,Mod Rewrite,Apache2,我为一个网站设置了几个“域名别名”,我将其用作无cookie子域,因此static.domain.com/style.css提供与www.domain.com/style.css相同的文件 但是,如果有人试图访问static.domain.com/index.htm,他们应该被301重定向到www.domain.com/index.htm。我在根web目录中拥有的mod_rewrite规则我认为可以工作,但它们似乎不起作用 <IfModule mod_rewrite.c> R

我为一个网站设置了几个“域名别名”,我将其用作无cookie子域,因此
static.domain.com/style.css
提供与
www.domain.com/style.css
相同的文件

但是,如果有人试图访问
static.domain.com/index.htm
,他们应该被301重定向到
www.domain.com/index.htm
。我在根web目录中拥有的
mod_rewrite
规则我认为可以工作,但它们似乎不起作用

<IfModule mod_rewrite.c>
    RewriteEngine On

    # "/res/all.20110101.css" => "/res/all.css"
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|jpeg|gif)$ $1.$3 [L]

    # Except for "static" sub-domains, force "www" when accessed without
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
    RewriteCond %{HTTP_HOST} !^s-img\.domain\.com [NC]
    RewriteCond %{HTTP_HOST} !^static\.domain\.com [NC]
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

    # If file requested is HTML, force "www" 
    <FilesMatch "\.(htm|html|php)$">
        RewriteCond %{HTTP_HOST} .
        RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
        RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    </FilesMatch>

</IfModule>

重新启动发动机
#“/res/all.20110101.css”=>“/res/all.css”
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(+)\(\d+)\(js | css | png | jpg | jpeg | gif)$$1.$3[L]
#除“静态”子域外,在访问时不使用
重写cond%{HTTP_HOST}。
重写cond%{HTTP_HOST}^www\.domain\.com[NC]
重写cond%{HTTP_HOST}^s-img\.domain\.com[NC]
重写cond%{HTTP_HOST}^静态\.domain\.com[NC]
重写规则(.*)http://www.domain.com/$1[R=301,L]
#如果请求的文件是HTML,则强制“www”
重写cond%{HTTP_HOST}。
重写cond%{HTTP_HOST}^www\.domain\.com[NC]
重写规则(.*)http://www.domain.com/$1[R=301,L]

这将重定向每个不适用于静态文件的请求:

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
内容如下:

  • 如果主机名不是www.domain.com
  • 请求的文件未以允许的扩展名结尾
  • 然后重定向到主(www)域
对于您的版本控制(您需要非贪婪的
(.+?)
,因为
+
会吃掉您的整个字符串,并且没有匹配的模式):


在对vbence的答案进行了一番讨论之后,我偶然发现了一个更实用的解决方案,尽管我不确定它是否是最优化的解决方案

<IfModule mod_rewrite.c>
   RewriteEngine On

   # Requests for "/res/all.20110101.css" serve up "/res/all.css", etc.
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.+?)\.([0-9]+)\.([a-z]+)$ $1.$3 [L]

   # If the hostname isn't www.domain.com AND requested file's extension
   # isn't in the filter list, redirect to the www.domain.com version.
   RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
   RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpe?g|gif)$ [NC]
   RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

</IfModule>

重新启动发动机
#请求“/res/all.20110101.css”提供“/res/all.css”等。
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(+?)\([0-9]+)\([a-z]+)$$1.$3[L]
#如果主机名不是www.domain.com和请求的文件扩展名
#不在筛选器列表中,请重定向到www.domain.com版本。
重写cond%{HTTP_HOST}^www\.domain\.com$[NC]
重写条件%{REQUEST\u FILENAME}!\。(js | css | png | jpe?g | gif)$[NC]
重写规则(.*)http://www.domain.com/$1[R=301,L]

只是为了澄清你的规则:像
/res/all.20110101.css这样的文件不存在,而且这个数字只是为了缓存,对吗?@nickgrim:对,这个规则是为了灵活的文件名,所以缓存破坏比使用查询字符串更可靠。为版本控制添加了单独的规则。谢谢。我无法使用第一个代码块启用第二个静态子域。我尝试复制第一行并相应地更改子域,但没有成功。无论如何,在你的一点帮助下,我搞乱了这一切,最终得到了一个更紧凑、功能更强大的mod_重写规则集。我会用它来回答我自己的问题,因为它更全面地解决了我的问题。你没有说你需要第二个域名。我在你的代码示例中看到了它,但由于你在问题中没有提到它,所以我将其驳回。无论如何,我建议不要使用过于紧凑的规则。以后更改它们通常很麻烦。我已经修改了第一条规则,将所有子域的非静态内容重定向到主站点。可能不清楚,但问题的第一句中提到了它们。我建议在
之后添加
$
^www\.domain\.com
。用于创建子域(如
www.domain.com.someotherdomain.com
)时极不可能出现的情况。越具体越好。
<IfModule mod_rewrite.c>
   RewriteEngine On

   # Requests for "/res/all.20110101.css" serve up "/res/all.css", etc.
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.+?)\.([0-9]+)\.([a-z]+)$ $1.$3 [L]

   # If the hostname isn't www.domain.com AND requested file's extension
   # isn't in the filter list, redirect to the www.domain.com version.
   RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
   RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpe?g|gif)$ [NC]
   RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

</IfModule>