redirect external subdomain traffic - apache

I have apache installed on one of my server on two different ports, all http requests are handled by the apache on the default (80). Now I have a subdomain for a domain and I want the request for the sub domain to be handled by the apache on the other port (10024).
I've tried using
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^sub.domain.com$ [NC]
RewriteRule .* http://www.domain.com:10024/dir/page.php [R,L]
but dont get the required result.
Any suggestions?
Thanks in advance

Try changing HTTP_REFERER to HTTP_HOST and rewrite rule to:
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^/$ http://www.domain.com:10024/dir/page.php [R,L]

Related

.htaccess - SSL redicect condition for one specific domain not working

I have an apache server with multiple websites hosted:
main-website.com
subdomain1.main-website.com
subdomain2.main-website.com
another-website1.com
another-website2.com
another-website3.com
I need to redirect only https://www.main-website.com to https://www.main-website.com
The subdomains and all the other websites don't need a ssl certificate; therefore I want to exclude them from redirection by specifying that only main-website needs to be redirected.
This is my .htaccess syntax (it seems correct having researched a lot on this topoic)
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The condition of redirecting main-website.com has been specified, but it does not work! In fact every other domain and subdomain is redirected to HTTPS!!
Do you know where the error could be?
Thank you :)
There must be a typo in your question, you want to redirect from https://www.main-website.com to https://www.main-website.com I will take for granted you wanted to say http://www.main-website.com to https://www.main-website.com
So, your first RewriteCond-RewriteRule set rewrites:
somesite.com ---> https://www.somesite.com
But notice that you also force https in that rediction. Therefore any attempt to reach http://somesite.com will go to https://www.somesite.com
It is probably this rule set that is triggered by your other domains. That first rule should be to rewrite: non www to www, without forcing https at that point.
Then the next rule set will apply only to your main-website and send it to https.
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have updated the first set of instructions. Still, it does not work :(

htaccess rewrite multiple conditions

I don´t know how to solve this problem. I need to redirect from http to https but only in some cases:
Redirect
http://example.com to https://example.com
http://example.com/any-url to https://example.com/any-url
Don´t redirect
http://subdomain1.example.com
http://subdomain2.example.com
http://example.com/any-file.xml
Im turning on ssl but only my domain has a certification, and i like to keep xml files without redirection to avoid some partners issues.
Any help?
This rewrite should work:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} example.com
RewriteCond %{REQUEST_URI} !\.xml$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
In your case, this would work, assuming your regular HTTP connection is port 80:
# If the site is plain HTTP
RewriteCond %{SERVER_PORT} 80
# and if the requested filename is not an XML file
RewriteCond %{REQUEST_FILENAME} !^(.*)\.xml$
# and if the URL specifies the request is for the primary domain (not subdomain)
RewriteCond %{HTTP_HOST} ^example.com$
# then rewrite to HTTPS
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
You would need to of course insert the correct domains, but it should work. I tested it on my system and it did.

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

apache mod rewrite for subdomain

On my webserver, I have one website located at example.com/site (in /var/www/site).
I want it to be accessed through site.example.com, and I'm aware I need to use mod-rewrite to enable this.
Is there a concise snippet I can use to do this? I need to make it extensible so other sites can be accessed this way as well. Thanks in advance.
If this is a matter of one specific subdomain: site, then you should explicitly rewrite just that one subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L]
The second condition prevents an internal rewrite loop and making your URI look like /site/site/site/site/site/site/ etc...
This would go in your .htaccess file in your document root, /var/www/
If you really want to arbitrarily redirect any subdomain, you can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1 [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
Here, the second condition serves the same purpose, it tries to make sure the request URI doesn't start with the subdomain match from the host. See https://stackoverflow.com/a/11508430/851273 for a better explanation.
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]

redirect http to https for some page in site in APACHE

I want to one of my site's page will use only HTTPS.
I have given manually link to all sites to https.
But I want that if user manually types that page URL with http then it should be redirected to https page.
So if user types:
http://example.com/application.php
then it should be redirected to
https://example.com/application.php
Thanks
Avinash
Here's a couple of lines I used in an .htaccess file for my blog, some time ago :
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{REQUEST_URI} ^/admin*
RewriteCond %{HTTPS} !=on
RewriteRule ^admin/(.*)$ https://www.example.com/admin/$1 [QSA,R=301,L]
Basically, the idea here is to :
determine whether the host is www.example.com
and the URL is /admin/*
Because I only wanted the admin interface to be in https
which means this second condition should not be useful, in your case
and https is off (i.e. the request was made as http)
And, if so, redirect to the requested page, using https instead of http.
I suppose you could use this as a starting point, for your specific case :-)
You'll probably just have to :
change the first and last line
remove the second one
Edit after the comment : well, what about something like this :
RewriteCond %{HTTP_HOST} =mydomain.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://mydomain.com/$1 [QSA,R=301,L]
Basically :
using your own domain name
removing the parts about admin
Try this rule:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^application\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This rule is intended to be used in the .htaccess file in the document root of your server. If you want to use it in the server configuration file, add a leading slash to the pattern of RewriteRule.
use this:
RewriteEngine On
# Turn SSL on for /user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/user/login
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
# Turn SSL off everything but /user/login
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/user/login
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [R=301,L]
website
open httpd.conf or .htaccess file (mod_rewrite not required):
# vim httpd.conf
Append following line :
Redirect permanent / https://example.com/