简体   繁体   English

.htaccess条件:https,www,重写

[英].htaccess conditions: https, www, rewrite

Would like to do the following on my website using .htaccess rewrite mod: 想要在我的网站上使用.htaccess rewrite mod进行以下操作:

  • redirect users without https://* to https://* 将没有https:// *的用户重定向到https:// *
  • redirect users with www.example.com to example.com 使用www.example.com将用户重定向到example.com
  • rewrite example.com/index.htm to example.com/home example.com/index.htm重写为example.com/home
  • redirect every URI to index.htm exept of /home , /background.jpg , /icon.png and /fonts/segoeui.ttf 将每个URI重定向到/ home/ background.jpg/ icon.png/fonts/segoeui.ttf的 index.htm

This is the code I already have: 这是我已经拥有的代码:

RewriteEngine On
RewriteBase /

RewriteRule ^\.htaccess$ - [F]

# --- Part that looks stupid but is working ---
RewriteCond %{SERVER_PORT} !^443 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# --- Part that is not really working ---
RewriteCond %{REQUEST_URI} !^/(index\.htm)?$ [NC]
RewriteRule ^.*$ /index.htm [L,R=301]

So, is there a better way to combine the https and the www thing? 因此,是否有更好的方法将httpswww结合起来? It is more a copy and paste solution than good code. 它比有效的代码更像是复制和粘贴解决方案。 I have not much knowledge with this. 我对此并不了解。

The redirection to index.htm is working, but how can I prevent /background.jpg , /icon.png and /fonts/segoeui.ttf from being redirected too? 重定向到index.htm的工作,但我怎么能防止/background.jpg,/icon.png/fonts/segoeui.ttf被重定向吗? And what about rewriting the whole thing to /home ? 然后将整个内容重写到/ home呢?


Thanks for your help! 谢谢你的帮助!

You can combine first two conditions in only one 您只能将前两个条件组合在一起

# Force https and non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Then, rewrite example.com/index.htm to example.com/home 然后, 将example.com/index.htm重写为example.com/home

# Redirect /index.htm to /home and avoids infinite redirect loop
RewriteCond %{THE_REQUEST} \s/index\.htm\s [NC]
RewriteRule ^ /home [R=301,L]

# Rewrite (internally) /home to /index.htm
RewriteRule ^home$ /index.htm [L]

Finally, redirect every URI to index.htm exept of /home, /background.jpg, /icon.png and /fonts/segoeui.ttf 最后, 将每个URI重定向到/home、/background.jpg、/icon.png和/fonts/segoeui.ttf的index.htm库

# Redirect (if not an existing file) to /index.htm (which will after redirect to /home)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.htm [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM