I got a quick question on redirecting to HTTPS.
I want
to
to
How can I make this possible? The point is, that I got several Domains linking to one Directory and I only want to use one single SSL Certificate.
Thanks to everyone out there who can help me :-)
Kind regards
After hours of searching today I finally found a solution. Maybe it is useful for you so I would like to share:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.tld$
RewriteRule ^ https://%1.domain.tld%{REQUEST_URI} [NE,L,R]
:-)
Related
I seearched a little bit but seems like I cant find proper answer for my problem. I have 4 domains and I need to redirect 3 domains (and some of their pages) to new one.
https://www.domain1.com/ to https://www.domain4.com/music/domain1/
https://www.domain2.com/ to https://www.domain4.com/music/domain2/
https://www.domain3.com/ to https://www.domain4.com/music/domain3/
and i need it for some pages also, for example:
https://www.domain1.com/about.php to https://www.domain4.com/domain1/about/
Can someone help me how to do this in htaccess?
Thanks in advance
Could you please try following, written as per shown samples. After placing these Rules into your .htaccess file, before testing make sure you clear your browser cache.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain[123])\.com [NC]
RewriteCond %{REQUEST_URI} ^/?$
REwriteRule ^(.*)$ https://www.domain4.com/music/%1/ [NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain[123])\.com [NC]
RewriteCond %{REQUEST_URI} ^/(about)\.php/?$
REwriteRule ^(.*)$ https://www.domain4.com/%1/%2/ [NE,L]
For reasons much too long and complex to get into (it involves several layers of corporate red tape resulting in someone else not purchasing a wildcard SSL certificate I requested), I have to set up a domain to redirect all requests to https://www.xyz.com – secure protocol with the www subdomain.
So: http://xyz.com, http://www.xyz.com, and https://xyz.com should ALL redirect to https://www.xyz.com.
My .htaccess-fu is weak at best and I can't seem to get this to work. Note: hosting is on Media Temple if that makes a difference.
So far, my .htaccess file looks like so:
RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://www.xyz.com/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Both http://xyz.com and http://www.xyz.com are redirecting to https://www.xyz.com, so yay. However, https://xyz.com is not redirecting to https://www.xyz.com and is thus throwing a security warning page:
This is probably not the site you are looking for!
You attempted to reach xyz.com, but instead you actually reached a server identifying itself as www.xyz.com. This may be caused by a misconfiguration on the server or by something more serious. An attacker on your network could be trying to get you to visit a fake (and potentially harmful) version of xyz.com.
You should not proceed, especially if you have never seen this warning before for this site.
Any help in getting me past this one final hump would be muchly appreciated!
This is very much possible. use the following code. this works for me.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
In case anyone from the future (HELLO PEOPLE OF THE FUTURE) stumble across this, I asked the same question and had it answered over at Server Fault.
Short version: impossible.
Long version: https://serverfault.com/questions/523199/redirect-all-http-and-https-non-www-urls-to-https-www-example-com-via-htaccess
Even though this issue is very old.
If found it googeling the exact same issue. After the Code provided here didn't help. I tried and tried.
This one worked for me:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
Fixed the issue for me without any Loops or other errors. Maybe it will help someone else.
I sincerely do not understand #scottie link saying IMPOSSIBLE being the accepted answer.
I am using below codes on 3 of my websites, provided here by Erik and it successfully makes
http://example.com, http://www.example.com, https://example.com all land on https://www.example.com
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
P.S: I had to #RewriteEngine On because I already have RewriteEngine On at the beginning of my .htaccess
I hope this also helps someone like me who might have thought its a really long process, and if by any chance there is a shortcoming of this I am not aware, kindly let me know.
My server provider stopped give me possible way to create subdomains in control panel and they say I should solve it with htaccess.
So I have to do something like this. When user types http://asdf.example.com it should get the content of http://example.com/asdf (as the provider changed even the FTP's structure... how kind of them)
I don't find any tutorial for this. Thanks in advance.
More generic version of TerryE's answer. I haven't tested it though!!!
RewriteCond %{HTTP_HOST} !www.example.com
RewriteCond %{HTTP_HOST} (.*).example.com
RewriteCond %1/$0 !^([^/]+)/\1
RewriteRule ^.* %1/$0 [L]
You can do that with mod_rewrite and mod_proxy:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^asdf\.example\.com$
RewriteRule ^ http://example.com/asdf%{REQUEST_URI} [L,P]
You don't need mod proxy. Just do an internal redirect:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =asdf.example.com
RewriteCond $0 !^asdf/
RewriteRule ^.* asdf/$0 [L]
The rule rewrite the request prefixing asdf/ but only if (1) the host is http:/asdf... and (2) the rewrite hasn't already taken place. You need (2) to prevent iterative loops. $0 is the match string in the rule.
Hope this helps.
I have found similar questions, however no-matter what I try I am unable to get htaccess to do what I need it to. What I'd like is to direct visitors to an SSL connection when visiting one (or more) virtual subdirectories and then reverse this when they leave these selected subdirectories.
My rules look like this:
Options +FollowSymlinks
RewriteEngine on
#switch on SSL in /directory/ only
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/directory/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#switch off SSL in other folders
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/directory/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^([^/\.]+)/?$ /index.php?var1=$1 [NC,L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+).html /index.php?var1=$1&var2=$2 [NC,L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?var1=$1&var2=$2 [NC,L,QSA]
It seems that switching on SSL and switching off SSL conflict and when I try to access any page via https://www.domain.com/foo/ I am correctly redirected to http://www.domain.com/foo/.
However, when trying to access http://www.domain.com/directory/ (which should be SSL) I am redirected to http://www.domain.com/index.php?var1=directory and not https://www.domain.com/directory/.
I would really really appreciate a bit of guidance over where I'm going wrong. I've trawled through stackoverflow and the web for hours and hours looking for an answer but I'm just going round in circles and can't see the wood from the trees.
Thank you for any guidance/help you can offer.
I'm sorta a noob at these things but I'm trying to make a simple virtual subdomain with .htaccess. I have wildcard enabled and after lots of digging, this is what I've come up with:
rewriteEngine On
rewriteCond %{HTTP_HOST} !^$
rewriteCond %{HTTP_HOST} !^(www\.)?khpedia\.com$ [NC]
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
rewriteRule ^(.+) /%2/$1 [L]
My directory is setup as
-root
--wiki
----index.php
--test
Right now when I travel to wiki.khpedia.com, I get a page not found. When I travel to wiki.khpedia.com/index.php, it travels to wiki.khpedia.com/wiki/index.php. I am somehow also able to access wiki.khpedia.com/test. If it doesnt seem obvious yet, I want to be able to go to wiki.khpedia.com/index.php and see wiki.khpedia.com/wiki/index.php but not in my address bar. Sorry for the text block and thanks for the help.
This works on the Apache side:
RewriteCond %{HTTP_HOST} !^www\.khpedia\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).khpedia\.com$ [NC]
RewriteRule ^(.*) /%1/$1 [L,QSA]
But you're wiki software might make up it's own mind about redirects / urls.
RewriteCond ^(.*)$ /wiki/$1 [L]
I'm confused about where subdomains come into your question. Could you give some examples of URLs you want to access in the browser and what they should point to at the server?
EDIT | Ah, I see now, Wrikken's answer handles the subdomains correctly :)