Using htaccess, Redirect http or https domain, and any subpage to particular page on another server - apache

Background:
We have DNS pointing to new server.
Apache Server has a directory called example
Example directory has an htaccess file for redirecting, code below
Issue is : We need this to redirect any variation of the url ie: http://example.com, https://example.com, http://example.com/filename.html, https://example.com/directory/filename.html, etc.. to a specific page on another server.
So far, we have the code below, it works correctly for https, but not http.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://differenturl/login/login.html [R=301,L]
I feel like there is a simple answer but can't seem to find it!
**Edit: for clarification -by not working, I mean that I am not redirected to https://differenturl/ etc... using http. It only works for https. http routes me to the home page of the server that this all sits on. And that server has a different domain name than anything above.

If it's "not working" for HTTP then either:
The <VirtualHost> container for port 80 (ie. HTTP) is not configured at all in the server config.
Or,
The vHost:80 container is pointing to a different area of the filesystem, so the .htaccess file is not processed.
Or,
The vHost:80 container does not permit .htaccess overrides so the .htaccess file is ignored. You either need to configure .htaccess overrides by setting AllowOverride All in the appropriate <Directory> container in the vHost:80 container. Or simply redirect everything to HTTPS in the vHost:80 container (eg. Redirect / https://example.com/) and then allow the .htaccess file to redirect from HTTPS. (That's potentially 2 redirects, but that should not be an issue.)
Or just do the redirect (to the other server) in the server config and not use .htaccess at all.

Related

Redirect non-www to www https Apache XAMPP

Please forgive what may sound like a similar question to what has been asked, however, I am VERY new to XAMPP and Apache and I have tried all possible combinations of the Rewrite Rules mentioned in other threads, which I have placed in the httpd.conf, httpd-default.conf and also in .htaccess and I simply cannot get the rewrite rules to work.
I simply want to redirect example.com to www.example.com. Please note that my redirect from HTTP to HTTPS is working 100%, so if someone can please advise on exactly where I should place the rewrite rule, and which one to use, to force non-www to www, I would be most appreciative.
I have full access to the server so I can edit either .conf or .htaccess files. Also I have checked and the part in httpd.conf that allows overrides to read the .htaccess files is enabled. The .htaccess file is currently in the same folder where my website lies. Just to add, I don't get any error, it just says the page has timed out if I type in example.com, if I type www.example.com then the page loads as expected.
Try this in your .htaccess file:
Replace example.com with your url.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
The connection has timed out
This is a DNS or server config error. The request is not even reaching your server, or your server is not responding to the request. (So any attempt to implement a redirect will naturally fail.)
A quick check of the DNS for example.com and www.example.com shows they are pointing to different IP addresses (you have an A record configured for each). The example.com host is 1 digit different and would appear to be incorrect (the IP address does not respond to requests, hence the "time out").
Instead of having a separate A record for each hostname. If the www subdomain is supposed to point to the same site as the domain apex (ie. example.com) then configure a CNAME record instead that points www.example.com to example.com (this is not a "redirect").
You then need to ensure that your server will accept requests to the non-www hostname. For example:
ServerName example.com
ServerAlias www.example.com
(It's possible you have the ServerName and ServerAlias reversed - which is "OK".)
And the SSL cert needs to cover the non-www hostname (it does).
You can then implement your non-www to www HTTP redirect as mentioned.

My homepage wont go through HTTPS port

So my domain is [1] and as you can see whenever you go on that link it will get HTTP (without padlock) but if you go to any other link it will be HTTPS. Only my homepage goes through HTTP.
Important things to mention is that I use Apache.
Every my attempt to edit .htaccess file ends up by website stoping to work. My whole website is hosted on AWS and that is where I derived my SSL certificate.
I want to make that homepage load in HTTPS as well.
How should I do that?
Here is my app folder and place where I created my .htaccess file.
[1]:
Adding this to your .htaccess should do it:
RewriteEngine on
RewriteCond %{HTTPS} =off
RewriteRule ^$ https://www.urtina.com/ [L,R=301]
If it breaks Apache, check the error log. It is for the homepage only as requested.

Rewrite rule for subdomains

Is it possible to use .htaccess for a subdomain to point to a file on the main domain?
I have example.com as a main domain and created subdomains: one.example.com, two.example.com, etc through DirectAdmin. The subdomains should point to app.php on the main domain but in the url the subdomain should persist.
So navigating to one.example.com should be pointing to app.php which lives in the main domain example.com, but the url should still show one.example.com
I have looked and tried a lot of the rewrite rules found on .htaccess rewrite subdomain to directory and elsewhere.
Currently I have this rewrite rule in a .htaccess file in the subfolder of the subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/app.php$1 [L,NC,QSA]
This redirects and changes the url to example.com/app.php while I would like the url to show the subdomain: one.example.com/whatever
Is it possible to do this with .htaccess ,or is there maybe even a better way to do this?
A RewriteRule with a target on another server will always do a redirect
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
To map the URL http://example.com/app.php into your subdomains without redirecting, you might try the P|proxy flag
RewriteRule ^(.*)$ http://example.com/app.php/$1 [P,QSA]
Also keep the final note in mind
Note: mod_proxy must be enabled in order to use this flag.
Usually, this can be done by
a2enmod proxy proxy_http
If the target URL is https://..., you must also enable mod_proxy_connect, and activate SSL in your main or virtual host config with SSLProxyEngine
SSLProxyEngine on
Depending on your needs, ProxyPass might be a viable solution too.

Redirection https to https within same Apache

I have a requirement where I have to redirect my hostname to particular application which is again hosted on same Apache. Let's take an example, When I hit on host(https://domain1.example.com), It should internally redirect me to Apache Web Application (https://domain1.example.com/application1) without changing the browser URL.
I am not sure how to achieve SSL to SSL redirection. Thanks in Advance..!!!
This should work. This will redirect all incoming urls that are going to domain1.example.com/ to domain1.example.com/application1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.example.com$
RewriteRule ^$ https://domain1.example.com/application1 [L,R=301]
If without changing browsing URL is your goal then PROXY is your way.
Put following in your apache vhost or global file,
ProxyPass https://domain1.example.com/ https://domain1.example.com/application1
ProxyPassReverse https://domain1.example.com/ https://domain1.example.com/application1
PS shibboleth has nothing to do with this, at least you have not mentioned any case.
EDIT
ProxyPass should come to virtural host not in location
Ideally all the location tag should be out of virtual host

Redirect non-www to www not working

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code
RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]
restarted apache and no effect and then tried
<VirtualHost *:80>
ServerName megahotserved.com
Redirect permanent / http://www.megahotserved.com/
</VirtualHost>
in the httpd.conf file, when I restarted apache firefox came up with an error
The page isn't redirecting properly.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
What should I do ?
your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:
RewriteEngine On
RewriteOptions Inherit
Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.
Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.