views:

29

answers:

1

Hello everyone,

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.mydomain.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

+1  A: 

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]
Tim Stone
I tried a copy paste and I had an error 500.First group works (http -> https) but not the rest
Alright, I'll throw this on my test server in a bit and find out what might be going wrong. Once I do I'll update my answer and let you know.
Tim Stone
OK, you can see if that works for you.
Tim Stone
That works great now !!! Do you know a good way to debug that ? It is not quite easy
Awesome. Well, in this case it was a fairly trivial error on my part (although I made some other changes), and probably would have shown up in the server's `error_log` file. But usually I use the `RewriteLog` to see exactly what's going on at each step when it's not working like I expect. You have to have control of the server for this though, so unfortunately people can't do it on their shared hosts to provide feedback. I use it on my local test server though to work out as many kinks as possible for people.
Tim Stone