Apache 简单的mod_重写重定向

Apache 简单的mod_重写重定向,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,经过大量的研究和帮助,我终于了解了一点mod rewrite的工作原理。。。有人能告诉我下面的代码有什么问题吗 ######redirect all static files to the static domain RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC] RewriteRule ^(.*)$ http://static.example.com/$1 [R=301,L] ######

经过大量的研究和帮助,我终于了解了一点mod rewrite的工作原理。。。有人能告诉我下面的代码有什么问题吗

######redirect all static files to the static domain
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://static.example.com/$1 [R=301,L]

######redirect naked to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

######redirect IP to www
RewriteCond %{HTTP_HOST} ^100\.100\.100\.100$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

######redirect all non static files from static domain (because that remains) to www
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
谢谢

还有,这两者之间的区别是什么:

RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]

试试这个。htaccess:

######redirect all static files to the static domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule \.(gif|png|jpe?g|jfif|bmp|css|js)$ http://static.example.com%{REQUEST_URI} [R=301,L,NC]

######redirect naked to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^100\.100\.100\.100$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

######redirect all non static files from static domain (because that remains) to www
RewriteCond %{HTTP_HOST} ^static\. [NC]
RewriteRule !\.(gif|png|jpe?g|jfif|bmp|css|js)$ http://www.example.com%{REQUEST_URI} [R=301,L,NC]

根据我们之前的谈话。您还可以组合
www
IP
规则。它只需要检查
www
是否不存在

######redirect naked to www or IP
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

######redirect all static files to the static domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC] 
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://static.example.com/$1 [R=301,L]

######redirect all non static files from static domain (because that remains) to www
RewriteCond %{HTTP_HOST} ^images\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301

这是另一种方式。

你面临什么问题?@anubhava:
www.example.com/image.png
不会重定向到
static.example.com/image.png
@MainHero第一条规则应该为你做到这一点。你是说它现在不工作了吗?@Panama-Jack:好像和我的浏览器有关。。。我遇到的第二个错误是无休止地重定向到static.example.com。你能解释一下
^(www\)?
的作用吗?它可以匹配
www
,也可以选择匹配
www.example.com
example.com
######redirect naked to www or IP
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

######redirect all static files to the static domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC] 
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://static.example.com/$1 [R=301,L]

######redirect all non static files from static domain (because that remains) to www
RewriteCond %{HTTP_HOST} ^images\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301