Apache Redirects in HTTPS - apache

I have an Apache server (https://mysite.com/sub/en/). It's setup with SSL on port 443, and in the VirtualHost tags:
<VirtualHost _default_:443>
Redirect permanent /sub https://mysite.com/sub/en/
...
</VirtualHost>
Basically, when the user comes to the site, I want them to be forced to the "/en" sub-directory. However, if you try to visit https://mysite.com/sub, it doesn't redirect you to the sub "/en" directory.
I'm really not sure what is going on. I do have a similar setup in my non-ssl virtualhost tags:
<VirtualHost *:80>
Redirect permanent / https://mysite.com/sub/en/
Redirect permanent /sub https://mysite.com/sub/en
</VirtualHost>
These rules work fine. So if the user attempts http://mysite.com/sub, it forwards them to the SSL version, and the "/en" subdirectory like I want it to. But if you try the SSL version: https://mysite.com/sub, it fails to forward.
Do I have the configurations wrong? Am I not using the right redirect or rewrite rule?

To get this working the way I wanted, I ended up using a RewriteRule:
RewriteRule ^/sub$ https://mysite.com/sub/en/
This appears to have done the trick.

Related

Why is Apache 2.4 permanent redirecting (301) this to non www?

Why is Apache 2.4 permanent redirecting (301) this to non www?
<VirtualHost *:80>
ServerName www.domain.net
ServerAlias www.domain.com www.domain.org
Include conf/extra/domain/httpd-vhosts.domain.http
</VirtualHost>
The include file has the ordinary DocumentRoot, Directory, etc. But no redirect or rewrite conditions/rules.
.htaccess has some rewrite conditions/rules but nothing re: www. And no 301's.
Browser cache cleared and viewing with HttpWatch can see the non cached redirect from server on first use. Subsequent use the redirect is from cache.
Never mind. Found it buried in an earlier loading global conf.
Good thing I posted about it here or I probably never would have found it. ;)

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

Remap domain.io to show as domain.com in address bar

I have domain.io and domain.com BOTH pointing to the SAME server. How do I set this up so that domain.io never shows up in the address bar, and it's always domain.com?
Thanks!
You can use Apache's mod_alias Redirect directive for this:
<VirtualHost *:80>
ServerName domain.io
ServerAlias www.domain.io
Redirect permanent / http://domain.com/
</VirtualHost>
In your case, permanent (301) redirect sounds appropriate but you may want to look at HTTP redirect: 301 (permanent) vs. 302 (temporary)
Also, it's really unimportant where the domains are hosted in this case; they do not need to be on the same server. In fact, many DNS and web host providers actually provide this functionality as a service, and will host the equivalent of above configuration for you (though not necessarily with Apache; nginx is probably more likely for this purpose these days).

URL redirection in Apache rewrite

I installed a Aava shopping cart jadasite on a shared Java webhost and now my home page is like http://www.mydomain.com/jada/web/fe/localhost/English/home.
To make my home page appear at http://www.mydomain.com I need to make changes in the Apache's mod_rewrite rule. I don't know much about the Apache server.
Can you please tell me what should be the code for Apache's rewrite rule and where do I need to put it.
yes you could redirect with an htaccess file or you could upload an index.php file to your root and write this line of code
<?php
header('Location: http://www.mydomain.com/jada/web/fe/localhost/English/home');
?>
if you are sure you want this - put into .htaccess
RewriteRule ^(.*)$ /jada/web/fe/localhost/English/home/$1 [QSA,L]
However i feel there is server misconfiguration at your end. If you have direct access to apache configs you may want to set right vhosts. Here is example from my localhost:
<VirtualHost 127.0.0.2:80>
DocumentRoot X:/xampp/htdocs/mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
</VirtualHost>
127.0.0.2 should be your external IP address that apache is listening to. X:/xampp/htdocs/mydomain.com can be any folder you want (folders outside htdocs dir may need additional directives in order to work).

Redirect subdomain to subdirectory

i've already read all topics here and on google about redirection but i'm a programmer and i cant get it going.
I have Apache 2.2 installed. The web root is C:\Apache\htdocs. My network admin set me up a local domain that points to the server with Apache. The domain is myPhpApp.ourcompany.local. And this subdomain works, it shows the Apache "It works" page.
Now i have a website in C:\Apache\htdocs\myPhpApp and i want Apache to redirect the myPhpApp.ourcompany.local to this directory. The URLs should stay while browsing the website always as myPhpApp.ourcompany.local for example: myPhpApp.ourcompany.local/index.php, myPhpApp.ourcompany.local/data.php and so on.
I dont know how to achieve this? Mod-rewrite, virtual hosts, combination of both?
i have got this and this does not work:
<VirtualHost myphpapp.ourcompany.local>
DocumentRoot /myphpapp/
ServerName www.example1.com
RewriteEngine on
RewriteRule ^/$ /myphpapp/ [R]
</VirtualHost>
In effect i get:
Forbidden
You don't have permission to access
/myphpapp/ on this server.
Can anyone help?
EDIT
Maybe i forgot to mention: i dont put this into the www root which is C:/Apache/htdocs becuase i have more apps in there.
i have 3 directories in thdocs: myphpapp, myoldapp, mytestapp. As a target i want to have 3 subdomains that point to each directory.
I think it will be a combination of Nikola's and cromestant's answers:
<VirtualHost *:80>
DocumentRoot "C:/Apache/htdocs/myPhpApp"
ServerName myPhpApp.ourcompany.local
</VirtualHost>
First of all, you don't need a rewrite rule for simple thing as this.
I assume you need following VirtualHost definition
<VirtualHost *>
DocumentRoot /myphpapp/
ServerName myphpapp.ourcompany.local
</VirtualHost>
"VirtualHost *" part tells Apache on which interface and optionally port to listen to.
ServerName tells which domain name will be used to identify this virtual host.
Document root in your apache config should point to your directory where you have the app, in what you stated in your question it should be
DocumentRoot C:\Apache\htdocs\myPhpApp
and that is all.
restart or reload your apache, and test.