About Apache Rewrite Function to Shorten URL - apache

I have an URL "http://10.21.50.66:8123/test/file/index.htm" which hosted in Apache server. I would like to shorten the URL to just "http://10.21.50.66:8123/" and I managed to do it by using Rewrite function in httpd.conf. However, when I type "http://10.21.50.66:8123/" in browser, it will redirect and the URL of the browser will change back to the long url "http://10.21.50.66:8123/test/file/index.htm". What I would like to archive is that when every time I type "http://10.21.50.66:8123" in browser, the browser will open "http://10.21.50.66:8123/test/file/index.htm" but the URL on the browser will still showing "http://10.21.50.66:8123/". Below is the currecnt setting:
RewriteEngine On
RewriteRule ^/$ http://10.21.50.66:8123/test/file/index.htm [R,L]
Any ideas?
Thanks in advance.

If you use [R], the Apache will return response code 302 to the browser (with the new Location), which will cause that new URL to appear in address bar.
try:
RewriteEngine On
RewriteRule ^/$ /test/file/index.htm [L]

You need to create a virtual host in httpd.conf. You might find it in extras folder too as httpd.vhosts.conf Its something like this:
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
you need to write something like:
<VirtualHost 10.21.50.66:8123>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot [path to your test/file folder]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Related

Second VHost pointing to first on Apache Web Server

I followed this tutorial to get Vhosts working on my website. https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart. For my second site at reporting.mydev.site, my website still redirects to my main website at mydev.site. I am using Apache as my HTTP server, here are my two .conf files for my main server and my new Vhost.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mydev.site
DocumentRoot /var/www/mydev.site/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydev.site
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
And here is the .conf file for the vhost I cannot get to work
<VirtualHost *:80>
ServerName reporting.mydev.site
ServerAdmin webmaster#localhost
DocumentRoot /var/www/reporting.mydev.site/public_html
ServerAlias www.reporting.mydev.site
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I found the problem, the issue was that I needed to create the https version of this file using certbot "reporting.mydev.site-le-ssl.conf". my second vhost was pointing to the first only for the https version but for the http only version it was correctly pointing to my new website.

www.api.domain.com leads to one page and api.domain.com leads to another. www.domain.com works but domain.com doesn't. What gives?

I have the following Apache virtual hosts config file called 000-default.conf
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www/mydomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName api.mydomain.com
ProxyPreserveHost On
ProxyPass / http://xxx.xx.xx.xx:3000/
ProxyPassReverse / http://xxx.xx.xx.xx:3000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I am facing the 2 following issues and I'm not sure if the problem is on Apache's side or on the DNS's side:
When I visit www.mydomain.com , I see the page that I am supposed to
be seeing but if I try typing just domain.com in the search bar, I
get an error - This site can’t provide a secure connection
When I visit api.mydomain.com , I see the correct page which I'm
supposed to be seeing BUT when I try www.api.mydomain.com , I get
the page that is supposed to be on www.mydomain.com instead.
Can anyone shine some light on whether this is a result of bad Apache configuration or I need to do something on the DNS?
You would need to add two lines to your /etc/apache2/.conf
ServerAlias www.domain.com
Add this line under the ServerName

My subdomain keeps redirecting to my main domain

I am sure this must have been asked multiple times already, I researched it for like a day but still couldn't figure it out.
I have a domain - domain.com and now I want to add a subdomain - test.domain.com for it. I have created a virtual host file and DNS A record - test.domain.com pointing to my server IP. But then whenever I try to access my test.domain.com, it keeps redirecting to domain.com. My Apache2 virtual host files are configured as follow
domain.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<Directory "/var/www/domain.com">
AllowOverride All
</Directory>
domain.com-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.domain.com
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
test.domain.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/test.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.test.domain.com [OR]
RewriteCond %{SERVER_NAME} =test.domain.com
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<Directory "/var/www/test.domain.com">
AllowOverride All
</Directory>
I have enabled the conf files with the command a2ensite and restarted apache (and desperately my server droplet) multiple times already but still couldn't get it to work. I am running on Ubuntu 16.04.6.
Any advise would be much appreciated. Thank you very much in advance!
A minimalistic example would be along these lines:
<VirtualHost *:80>
DocumentRoot /var/www/test.example.com
ServerName test.example.com
ServerAlias www.test.example.com
</VirtualHost>
In this example you declare a virtualhost for test.example.com and also www.test.example.com.
Note that I am using example.com here instead of domain.com because of posting restrictions active on this website.
The RewriteRule directives in your subdomain don't seem to be right, at least syntactically. They may be causing the redirection problem.
So I would remove them and take care of redirects later. Start with a simple configuration, validate it and expand with more rules later.
Hint: you could use curl (from the command line on your server if not available on your PC) to test like this:curl -L --head "http://example.com".
Test the main domain name, then subdomain in both http and https. There is an option in Curl to ignore SSL/TLS errors if you encounter any.
See what happens and pay attention to the redirects if any (status code 301/302).
I prefer to use Curl for testing because the browser cache can play tricks on you.

Redirect sub domain A to https domain B

I've tried looking for a post related to my issue but I couldn't find a suitable one. If there is, let me know!
Here's the current situation that I'm facing now. I would like to redirect a domain,
example.hr
that is being used with another server and there's an SSL that comes with it.
Now, I have another server that comes with the domain,
example.co
and there's an SSL cert too.
I would like to redirect test.example.hr to https://example.co. How can I go about to do this? I'm testing it with the subdomain since the root domain is in used.
I've tried this method,
<VirtualHost *:80>
ServerName test.example.hr
ServerAlias www.test.example.hr
Redirect / https://example.co/
</VirtualHost>
<VirtualHost *:443>
ServerName example.co
ServerAlias www.example.co
DocumentRoot /var/www/html
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/access.log combined
</VirtualHost>
The SSL configuration is inside the 443 block.
When I go to test.example.hr, it will change to https://test.example.hr and the error that comes up is "Your connection is not private. Attackers might be trying to steal your information from test.alt.hr (for example, passwords, messages or credit cards). Learn more
NET::ERR_CERT_COMMON_NAME_INVALID"
Can you try this and update me.
<VirtualHost *:80>
ServerName test.example.hr
ServerAlias www.test.example.hr
Redirect / http://example.co/
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName example.co
ServerAlias www.example.co
DocumentRoot /var/www/html
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/access.log combined
</VirtualHost>
make sure to enable a2enmod rewrite

apache2 VirtualHost subdomain configuration not working

All i want is, that www.example.com redirects to example.com, that works, but i can't add another VirtualHost which would allow admin.example.com for admins.
Here are my .conf file:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias old.website.com
DocumentRoot /var/www/redirect
<VirtualHost *:80>
ServerName admin.example.com
DocumentRoot /var/www/admin
and
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
However, if i enter admin.example.com i get redirected to example.com and if i use anything else as subdomain, for example test.example.com or anything.example.com the normal example.com site is shown BUT in the URL field you can still see the original URL (anything.example.com, ...) which isn't the case when using admin.example.com. My .htaccess file in the /var/www/redirect directory looks like that:
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
ErrorDocument 404 /index.html
Ok it seemed to be a google-chrome only error that was caused by my browser data, if anybody else is having problems with it, try using Icognito Mode in Chrome, it should work there.
Thanks to Chand Prakash for helping me