开发者

complex mod_rewrite. Any idea?

开发者 https://www.devze.com 2023-01-06 02:45 出处:网络
I want to : - switch from http to https if http is used - redirect the subdomain to index?o=subdomain except www

I want to : - switch from http to https if http is used - redirect the subdomain to index?o=subdomain except www - redirection the subdirectory to index?u=user

Example : http://www.mydomain.com will be redirected to https://www.mydomain.com

http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain

https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain

http://subdomain.mydomain.com/user will be redirected to https://www.mydomai开发者_高级运维n.com/index?o=subdomain&u=user

https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user

Is mod_Rewrite the best to do that ? Any idea ?

Thanks in advance


I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.

RewriteEngine On

# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]

Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):

If you wanted internal redirection:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2

If you wanted external redirection:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消