简单 mod_rewrite 重定向
simple mod_rewrite redirects
经过大量研究和一些帮助,我设法了解了一些 mod 重写的工作原理...有人能告诉我下面的代码有什么问题吗?
######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/ [R=301,L]
######redirect naked to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/ [R=301,L]
######redirect IP to www
RewriteCond %{HTTP_HOST} ^100\.100\.100\.100$
RewriteRule ^(.*)$ http://www.example.com/ [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/ [R=301,L]
谢谢!
另外,有什么区别:
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
和
RewriteCond %{REQUEST_FILENAME} !\.(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/ [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/ [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/ [L,R=301
这是另外一种方式。
经过大量研究和一些帮助,我设法了解了一些 mod 重写的工作原理...有人能告诉我下面的代码有什么问题吗?
######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/ [R=301,L]
######redirect naked to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/ [R=301,L]
######redirect IP to www
RewriteCond %{HTTP_HOST} ^100\.100\.100\.100$
RewriteRule ^(.*)$ http://www.example.com/ [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/ [R=301,L]
谢谢!
另外,有什么区别:
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
和
RewriteCond %{REQUEST_FILENAME} !\.(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/ [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/ [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/ [L,R=301
这是另外一种方式。