I have one application that listens on TCP 127.0.0.1:81.
I want to accomplish the following redirection:
www.example.com/?requestid=123456 --> http://127.0.0.1:81/?requestid=123456
www.example.com/ANYTHING_ELSE --> MY_IP_THAT_APACHE_LISTENS_ON
My understanding is that if I will not rewrite something explicitly, it will follow the regular path to the /var/www/html.
My /etc/apache2/sites-enabled/000-default.conf configuration:
<VirtualHost *:80>
ServerName example.com
ServerAdmin example#example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Location />
RewriteEngine On
RewriteRule ^/?requestid(.*)$ http://127.0.0.1:81/$1 [P]
ProxyPassReverse http://127.0.0.1:81/
Order allow,deny
Allow from all
</Location>
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Why it does not rewrite properly and keep hitting the normal patch?
Not Found
The requested URL /bullshit was not found on this server.
Apache/2.4.25 (Debian) Server at example.com Port 80
From RewriteRule Directive:
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
If you wish to match against the hostname, port, or query string,
use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or
%{QUERY_STRING} variables respectively.
So, you will need something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} requestid=(.+)
RewriteRule ^/$ http://127.0.0.1:81/?requestid=%1 [P]
Related
Ubuntu 18.04
Apache2
Certbot
I'm trying to get cerbot and non-www to www redirects set up on this site and I am copying the conf file from another one of my sites that is working just fine, but for some reason 443 is forbidden to the user on this new site and non-www.domain.url redirects to domain.url/www.domain.urlwww.domain.urlwww.domain.url etc.
main.conf
<VirtualHost *:80>
ServerName domain.url
Redirect permanent / https://www.domain.url/
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.url
ServerAdmin email#domain.url
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.url
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
main-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.domain.url
ServerAdmin email#domain.url
ServerAlias domain.url
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/user/project/static
<Directory /home/user/project/static>
Require all granted
</Directory>
<Directory /home/user/project/media>
Require all granted
</Directory>
<Directory /home/user/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/user/project/project/wsgi.py
WSGIDaemonProcess theprocess python-path=/home/user/prject python-home=/home/user/project/wow
WSGIProcessGroup theprocess
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.domain.url/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.domain.url/privkey.pem
</VirtualHost>
</IfModule>
ssh, http, https are all enabled with ufw. I might have a permissions issue with users being able to view my site, but it was working fine until certbot was installed. now everything (except non-https non-www which causes the repeating domain issue) redirects to https://www.domain.url and I get a forbidden message.
When I installed certbot I missed the non-www domain. when I went back and renewed to get both www and non-www the redirect setup failed but it said I had my certs. Is this causing the issue? I thought I could just build the redirect myself in the config file...
This post ended up being the answer:
https://serverfault.com/questions/957788/forbidden-after-enabling-ssl
I had a case error in my WSGIScriptAlias file path.
I am struggling with the https redirection of my domain. My domain is hosted with godaddy and my server is hosted with ionos. I was able to install the ssl certification and if I go directly on https: //domain.com it is working as expected. Unfortunately, the redirection is not working at all.
the conf file looks like that:
<VirtualHost *:80>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
and the ssl conf file like that:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} =domain.com [OR]
# RewriteCond %{SERVER_NAME} =www.domain.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
I hope someone has an idea regarding this.
Thanks a lot!
This should work, place it out of the virtualhosts (might not make a difference), and also make sure mod_rewrite is on.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Edit:
Added the flags R=301 and L for the redirect.
Info:
If the above doesn't work when you place it outside of virtualhosts, you should add the following after the RewriteEngine On:
RewriteOptions InheritDown
I have installed SSL Certificates on my website and on the example.com everything works fine, meaning that typing example.com redirects correctly to https://example.com. However, I have installed a certificate for a subdomain as well such that the link becomes: subdomain.example.com.
My goal is to have subdomain.example.com redirect to https://subdomain.example.com . This might sound weird but this semi-works meaning that when I first surf to subdomain.example.com it uses the http protocol but when I refresh that same page it switches to https protocol.
This is my VirtualHost conf file (port 80):
<VirtualHost *:80>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://example.com/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
RewriteCond %{SERVER_NAME} =subdomain.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/ [R=301,L]
</VirtualHost>
I have removed to non related lines from this sample above. Here is the 443 conf file:
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
< IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerName subdomain.example.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
#Allow forwarding to gitlab-workhorse
ProxyPassReverse http://127.0.0.1:8181
ProxyPassReverse http://domain/
</Location>
RewriteEngine on
#Forward all requests to gitlab-workhorse except existing files like error documents
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/subexample.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
SSLUseStapling on
Header always set Content-Security-Policy upgrade-insecure-requests
</VirtualHost>
</IfModule>
Worth noting is that I am using certbot.
Hopefully someone can help me.
You say "My goal is to have subdomain.example.com redirect to https://subdomain.example.com".
Then why have all that proxy configuration in your :80 VirtualHost? Simply force the redirection to :443, and let :443 handle the proxy (and other).
So your VirtualHost would become:
<VirtualHost *:80>
ServerName subdomain.example.com
CustomLog logs/subdomain_80_access.log combined
ErrorLog logs/subdomain_80_error.log
RewriteEngine On
RedirectMatch ^/(.*)$ https://subdomain.example.com/$1
</VirtualHost>
I am running apache 2.4.7 and presently all my traffic is going to a backend server on 8080 on the same server/instance.
I want my www traffic to go to a new server/instance and all my other subdomains (app, api, etc.) to go to the existing 8080.
Can somebody provide some direction?
Help appreciated.
<VirtualHost *:80>
ProxyPreserveHost On
ServerAdmin webmaster#example.com
ServerName example.com
Redirect "/" "https://www.example.com/"
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
Redirect "/ft/" "/"
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
If you want to do an external redirect to the new server for the www subdomain, you'll need to add the following to your configuration, under the other rewrite rules:
RewriteCond ${HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://www.example.com/${REQUEST_URI} [L,R=301]
And you would need to remove the ServerAlias directive. This is also assuming you've already pointed DNS to the new host.
If the new box serving the www content is to be another backed server, you would need another virtual host. Add the following at the top instead:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://other-box/
ProxyPassReverse / http://other-box/
</VirtualHost>
And you still would need to remove the ServerAlias directive.
I want my laravel app accessible with or without www on the url. like [http://laracasts.com][1]. when I hit http://example.com it works. But when I hit http://www.example.com, or http://www.example.com/contact I'm getting 404 error. How can I solve this? I'm using apache 2.4.7 on Ubuntu and here's my example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin foo#bar.com
DocumentRoot /var/www/myproject/public
<Directory /var/www/myproject/public/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
and here's my /etc/hosts file
127.0.1.1 example
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
xxxx::0 ip6-localnet
xxxx::0 ip6-mcastprefix
xxxx::1 ip6-allnodes
xxxx::2 ip6-allrouters
xxxx::3 ip6-allhosts
and .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
about server stuff, i'm noobs.
Your apache configuration is correct. However, you need to enable the rewrite module:
sudo a2enmod rewrite
sudo service apache2 restart
If you use a registered domain, this is enough. If you just use a "local" domain for development, you have to add this dumping domain into your hosts file at /etc/hosts. For example, when working with local environment, I usually use a domain .local, I'll add this line to make the domain example.local work:
127.0.0.1 example.local
You will need to include all the possible domain/subdomain name combinations that you would like to use in your hosts file.
127.0.0.1 www.example.com
127.0.0.1 example.com
In regards to http://www.example.com/contact does http://example.com/contact work?