简单的mod_rewrite重定向重定向、简单、mod_rewrite

2023-09-02 09:55:46 作者:2015萌夫。@

了大量的研究和一些帮助后,我设法学习了一下如何mod-rewrite工作......谁能告诉我有什么错我的code波纹管?

  ######重定向所有静态文件的静态域
的RewriteCond%{REQUEST_URI} ^ / (+)。(GIF | PNG | JPG | JPEG | JFIF | BMP | CSS | JS)$ [NC]
重写规则^(。*)$ http://static.example.com/$1 [R = 301,L]

######裸体重定向到www
的RewriteCond%{HTTP_HOST} ^ example.com $ [NC]
重写规则(。*)http://www.example.com/$1 [R = 301,L]

###### IP重新定向到www
的RewriteCond%{HTTP_HOST} ^ 100  .100  .100  .100 $
重写规则^(。*)$ http://www.example.com/$1 [R = 301,L]

######重定向从静态域的所有非静态文件(因为剩下的)到www
的RewriteCond%{REQUEST_FILENAME} !(GIF | PNG | JPG | JPEG | JFIF | BMP | CSS | JS)$ [NC]
重写规则^(。*)$ http://www.example.com/$1 [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]
 
Nginx中location匹配与rewrite跳转 详细版

解决方案

试试这个的.htaccess:

  ######重定向所有静态文件的静态域
的RewriteCond%{HTTP_HOST} ^(WWW )?例如 .COM $ [NC]
。重写规则(?GIF | PNG | JPE G | JFIF | BMP | CSS | JS)$ HTTP://static.example.com% {REQUEST_URI} [R = 301,L,NC]

######裸体重定向到www
的RewriteCond%{HTTP_HOST} ^例如 .COM $ [NC,OR]
的RewriteCond%{HTTP_HOST} ^ 100  .100  .100  .100 $
重写规则^ HTTP://www.example.com% {REQUEST_URI} [R = 301,L]

######重定向从静态域的所有非静态文件(因为剩下的)到www
的RewriteCond%{HTTP_HOST} ^静态。 [NC]
重写规则(GIF | PNG | JPE G | JFIF | BMP | CSS | JS?)$ HTTP:!//www.example.com% {REQUEST_URI} [R = 301,L,NC]
 

After a lot of research and some help, I managed to learn a bit of how mod rewrite works... Can someone tell me what's wrong with my code bellow?

######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]

Thanks!

Also, what's the difference between:

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

and

RewriteCond %{REQUEST_FILENAME} !.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]

解决方案

Try this .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]

 
精彩推荐
图片推荐