URL redirection in Apache rewrite - apache

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

Related

URL Rewrite in httpd.conf

I have a redirect setup on my Apache server within the httpd.conf file that redirects all traffic to "server.mydomain.com" to https://server.mydomain.com/uri. Unfortunately, some users will enter https before the URL and it does not redirect, instead they get Apache error page. I want to know how can I get users who input https before the URL to be redirect to https://server.mydomain.com/uri. I believe I may have to do a rewrite but I'm not sure or know how to go about doing it. I've research about rewrite and found it should be done in the .htaccess file but when I read Apache best practice they state it should not be done within .htaccess file for security and performance. Instead, it should be done within the config file. Since the redirection is working within the httpd.conf file, I would like to incorporate the rewrite there as well. I presume that is the correct according to Apache website. My issue is how do I go about doing this within that file. I've included the file information below. Any assistance is greatly appreciated. The server is using Tomcat 7 with Apache 2.2.15.
httpd.conf file:
</IfModule>
#
ProxyPass /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
ProxyPassReverse /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
</IfModule>
<VirtualHost *:80>
ProxyPass /uri http://server.mydomain.com:8080/uri
ProxyPassReverse /uri http://server.mydomain.com:8080/uri
Redirect permanent / https://server.mydomain.com/uri
</VirtualHost>
Did you try creating a vhost for 443 and switching to http there via Rewrite?
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

Apache Redirects in HTTPS

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.

Forward a subdomain to a subfolder and preserve subfolder's .htaccess mod_rewrite rules

I have a standard LAMP setup and I'm trying to make subdomains automatically work as long as the appropriately-named folder exists.
If you're familiar with MediaTemple's GridServer shared hosting service, I'm trying to emulate the way it handles wildcard subdomains:
If subdomain.domain.com is requested, look for a folder named subdomain.domain.com
If the folder is found, call it home and serve up the site inside of it, obeying any .htaccess files that may be in there.
If no folder is found, just display domain.com
I've been told a .htaccess mod_rewrite in my root domain is the way to go, and I've been able to detect subdomains and at least point to the appropriate subfolder, but I don't think this leaves any opportunity for the subdomain.domain.com .htaccess file's own mod_rewrite to take over once the server knows where the subdomain's folder is located.
Here's my .htaccess for that:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\.domain.com$
RewriteRule ^(.+) /vhosts/%1.domain.com/html/pages/$1.php [L,QSA]
...And my folder structure:
domain.com
html
.htaccess
vhosts
subdomain.domain.com
html
.htaccess
pages
index.php
pagename.php
So as you can see, the site inside subdomain.domain.com is dependent on it's own mod_rewrite in order to function, since the page files aren't where the server expects them.
Now, I know I could probably include the subdomain's .htaccess rules in my root domain's .htaccess with the appropriate conditions, but the kicker is that I need to be able to point completely different domain names to these subdomains too (so the subdomain is accessible via subdomain.domain.com and mydomain.com), so these subdomain.domain.com folders need to be totally self-sufficient.
So how can I get my server to look in the correct location for a subdomain's folder, while allowing its own .htaccess mod_rewrites to work?
Any help is greatly appreciated!
Just had someone suggest that mod_rewrites are the wrong way to go about it, and mod_vhs is what I want to use. Does anyone know about mod_vhs?
I got it to work using VirtualHosts:
NameVirtualHost *
# Root domain homepage
<VirtualHost *>
DocumentRoot /var/www/domain.com/html
ServerName domain.com
ServerAlias www.domain.com
</VirtualHost>
# Hosted sites
<VirtualHost *>
VirtualDocumentRoot /var/www/vhosts/%0/html
ServerName *
ServerAlias *
</VirtualHost>
The first entry catches requests to my home page and serves up the standard site, while the second catches EVERYTHING else and routes to a different location based on the host name requested.
Easy. Just make sure the subdomain is registered with your server's DNS, then point it with an htaccess like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule .* /subfolder [L]
No need to mess with the subfolder's htaccess.

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.

Apache multiple URL to one domain redirect

For the last two day, I've been spending a lot of time to solve my problem, maybe someone can help me.
Problem: I need to redirect different url's to one tomcat webbase-dir used for artifactory.
following urls should point to the tomcat/artifactory webapp:
maven-repo.example.local ; maven-repo.example.local/artifactory ; srv-example/artifactory
Where maven-repo.example.local is the dns for the server-hostname: "srv-example"
I'm accessing the tomcat app through the JK_mod module. The webapp is in the ROOT directory
This is what I've got so far:
<VirtualHost *:80>
#If URL contains "artifactory" strip down and redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^\artifactory\$ [NC]
# (how can I remove 'artifactory' from the redirected parameters? )
RewriteRule ^(.*)$ http://maven-repo.example.local/$1 [R=301,L]
ServerName localhost
ErrorLog "logs/redirect-error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName maven-repo.example.local
ErrorLog "logs/maven-repo.example.local-error.log"
CustomLog "logs/maven-repo.example.local-access.log" common
#calling tomcat webapp in ROOT
JkMount /* ajp13w
</VirtualHost>
The webapp is working with "maven-repo.example.local", but with "maven-repo.example.local/artifactory" tomcat gives a 404 - "The requested resource () is not available."
It seems that the mod_rewrite doesn't have taken any effect, even if I redirect to another page, e.g google.com
I'm testing on windows 7 with maven-repo.example.local added in the "system32/drivers/hosts" file
Thanks in advance!
Thanks a lot for your hint #PHP-Prabhu
a simple:
RedirectPermanent /artifactory /.
in the apache httpd.conf did the trick!
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence
Please see this URL
http://httpd.apache.org/docs/2.1/mod/mod_alias.html