簡體   English   中英

.htaccess條件:https,www,重寫

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

想要在我的網站上使用.htaccess rewrite mod進行以下操作:

  • 將沒有https:// *的用戶重定向到https:// *
  • 使用www.example.com將用戶重定向到example.com
  • example.com/index.htm重寫為example.com/home
  • 將每個URI重定向到/ home/ background.jpg/ icon.png/fonts/segoeui.ttf的 index.htm

這是我已經擁有的代碼:

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]

因此,是否有更好的方法將httpswww結合起來? 它比有效的代碼更像是復制和粘貼解決方案。 我對此並不了解。

重定向到index.htm的工作,但我怎么能防止/background.jpg,/icon.png/fonts/segoeui.ttf被重定向嗎? 然后將整個內容重寫到/ home呢?


謝謝你的幫助!

您只能將前兩個條件組合在一起

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

然后, 將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]

最后, 將每個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