Apache multiple URL to one domain redirect - apache

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

Related

Apache subdomain vhost + subfolders of several Magento 2 installs

Basically I want to have several stores under one subdomain as sub-folders. Like this:
mysubdomain.website.com/magento2v1 - Magento 2 store #1
mysubdomain.website.com/magento2v2 - Magento 2 store #2
mysubdomain.website.com/magento2v3 - Magento 2 store #3
Steps that I did:
This subdomain will be run in a VPS server so first I needed to redirect the subdomain (mysubdomain.website.com, the one above) to another IP (the actual VPS server) using an A record.
Second I created a Vhost with the mysubdomain.website.com on that VPS server
This is the vhost on the VPS server, Apache2:
<VirtualHost *:80>
RewriteEngine On
ServerName mysubdomain.website.com
ServerAlias www.mysubdomain.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mysubdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then in /var/www/mysubdomain I have:
/var/www/mysubdomain/magentov1
/var/www/mysubdomain/magentov2
/var/www/mysubdomain/magentov3
Now the problem is, Magento 2 needs .htaccess redirects where it redirects /pub to the actual sub-folder. I can't seem to get these .htaccess to work. The htaccess is read but it seems like the folders go a little bit crazy.
/var/www/mysubdomain/magentov1/.htaccess
/var/www/mysubdomain/magentov2/.htaccess
/var/www/mysubdomain/magentov3/.htaccess
Here's an example of this .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
Can anybody help me get this .htaccess to work? I think it's related to the folder structure, the folder base, but I don't know how to do this and didn't found any other example.
Thank you very much!
The issue most likely is that you rewrite to the absolute path /pub/ from within the configuration files in the subfolders. How should that rewritten request be processed? After that rewriting step it appears to the next round of rewriting like a request to https://sub.example.com/pub/........ None of your existing configuration files will get applied to that. And it most likely will lead to a http status 404. Which you should be able to see in your http server's error log file and also in your browser's console network tab.
Instead you should rewrite to the relative path pub/instead. Which would result in the next rewriting step getting applied to the request to https://sub.example.com/magentov2/pub/........ Which would again get (correctly) served from within that subfolder.
In general you should try to keep global rewriting rules in a common place instead of doubling them in various parallel configuration files. Even better than a common distributed configuration file (".htaccess") in the http hosts DOCUMENT_ROOT folder would be to implement such rule in the actual central configuration file. That is faster, more robust, more secure and easier to debug.

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>

Redirect all URLs to a single page in the httpd.conf

I am looking for a way to redirect an entire website to one single domain. I want to do this in the httpd.conf file. For example the VirtualHost I have looks like:
<VirtualHost xx.xx.xx.xx>
ServerName website.com
ServerAdmin webmaster#website.com
ScriptAlias /cgi-bin/ /usr/local/apache2/htdocs/cgi-bin/
DocumentRoot /usr/local/apache2/htdocs/website
CustomLog logs/website/access_log combined
ErrorLog logs/website/error_log
Redirect / http://newwebiste.com
Redirect /xxx http://newwebiste.com
</VirtualHost>
Currently the redirects above still keep the full URL.
So if I want to http://webiste.com/test/thispage.html I would redirect to http://newwebsite.com/test/thispage.html. I just want it to go to newwebsite.com and lose the rest.
So..
http://webiste.com/test/thispage.html
to
http://newwebsite.com
What do I need to add to the httpd.conf to achieve this?
Thanks,
As you have noticed Redirect catches all and sends the same URI to the target which is what you don't want, so you will use RedirectMatch, to match all possible requests, but not adding anything to the target url.
So you can just use:
RedirectMatch ^ http://newsite.example.com/
As a side note about mod_rewrite:
I certainly do not recommend mod_rewrite as recommended by others unless you really have a task at hand which requires the complexity only mod_rewrite can offer, and this is not the case.
Side note 2:
For examples it is better to use "example.com" domain name since it is not a valid domain and it is permanently reserved for example purposes.
Remove
Redirect / http://newwebiste.com
Redirect /xxx http://newwebiste.com
and add
RewriteRule ^ http://newwebsite.com [L,R=301]

How to redirect all/any page request on a site to a certain static page on same site

I need every page request on a site -- 200, 404, etc. -- to land at a fixed page on the site, in this case index_new.php.
I've tried other SO and ServerFault answers and I continue to get an endless redirect loop.
Here's my code in httpd.conf:
<VirtualHost *:80>
ServerName my.example.com
ServerAlias *.my.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index_new.php
RewriteRule ^(.*)$ /index_new.php [R=301,L]
</VirtualHost>
Notes:
the site's .htaccess file is empty
the "index_new.php" page is static with no PHP header("location:") internal redirects, etc.
I have restarted apache
A Negative lookahead should do, and can easily be done with a single line with RedirectMatch instead of using mod_rewrite:
RedirectMatch permanent ^/(?!index_new.php).* /index_new.php
Clear your browsers cache and/or try with curl -I http://my.example.com/wahtever to make sure it is working
For sanity specify documentroot in the virtualhost and a directory entry to allow access (Require all granted / Allow from all depending on the version) and since you are not using .htaccess (btw congratulations! you are one of the few doing things right from the start), set the directory of the DocumentRoot with "AllowOverride none" just to be safe.

How to redirect directory to another domain with Apache?

I have an Apache server with two domains (via VirtualHost) on it.
The file setup is like this: /example1/example2/
example1 is the root for www.example1.com
example2 is the root for www.example2.com
I would like for www.example1.com/example2/<whatever else> to redirect to www.example2.com/<whatever else>
I have access to the main Apache configuration file. Any tips on how to do this? I'm not very familiar with configuring Apache, so any explanations would be very much appreciated.
The other questions related to this one lacked explanations, so I felt I had to post my own question.
You can use an .htaccess file to do this pretty quickly. In www.example1.com/example2/, you would create this .htaccess file.
<VirtualHost *:80>
...
<Directory /path/to/vhost/>
RewriteEngine on
RewriteBase /example2/
RewriteRule ^(.*)$ http://www.example2.com/$1 [R=301]
</Directory>
</VirtualHost>
This does assume that you want the redirect to be a 301, and that the example2 folder has AllowOverride All set.
This rule works using regex to capture the incoming URL, sans the example2 bit, then appending it to the rewritten URL. The R in the square brackets tells Apache to use a Location header to the rewritten URL. The =301 tells Apache to use a 301 Permanent Redirect header.
If tou have mod_alias active, you can use the Redirect directive:
<VirtualHost *:80>
ServerName example1.com
Redirect 301 /example2 http://www.example2.com
</VirtualHost>