简体   繁体   中英

Combining htaccess redirects and rewrites (non-www to www, http to https, directory with keyword to root)

I (sometimes) somehow manage .htaccess file successfully but need help on this one.

I'd like to achieve the following at the same time

  1. Redirect anything non-www to www
  2. Redirect anything with /site or /site/ to root
  3. Redirect anything http to https

In the end, every link, no matter how it originated, should begin with https://www.domain.com/ and end with /original_request_directory_or_file_if_any

I can get #1, no problem

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/" [R=301,L]

I can get #2 no problem and it works together with #1

RedirectMatch 301 ^/site/$ http://www.domain.com/

What I am unable to achieve so far is #3

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

When I enable the above code, I get browser error for redirect loop errors.

Looking forward to a solution (because I am sure it's possible and I just don't know how.)

Thank you all in advance!

If you redirect all to https, avoid to redirect before to http.

RewriteEngine on
RewriteRule ^site/? https://www.domain.com/ [R=302,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]

Change [R=302,L] to [R=301,L] when everything works well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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