Redirect IP address to domain (Apache) - wordpress

I have installed my new website on an AWS EC2 instance and have an elastic IP. I have already enabled HTTPS for my site. At present, the domain loads with the website without any issue, but the IP points to the Apache default page. I followed several tutorials to point the IP address back to the HTTPS version of my site. But it's not working. But if I use https://xx.xx.xx.xx I get a "Your connection is not private" warning.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{REMOTE_ADDR} ^xx\.xx\.xx\.xx$
RewriteRule ^(.*)$ https://mynewwebsite.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Vhost:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#mynewwebsite.com
ServerName mynewwebsite.com
ServerAlias www.mynewwebsite.com
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mynewwebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mynewwebsite.com/privkey.pem
</VirtualHost>
</IfModule>

You have to define two VirtualHost with 443 ports.
One of this contains the same configuration for your application:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#mynewwebsite.com
ServerName mynewwebsite.com
ServerAlias www.mynewwebsite.com
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mynewwebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mynewwebsite.com/privkey.pem
</VirtualHost>
</IfModule>
One for redirect without ServerName and ServerAlias equal to wildcard (*).
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#mynewwebsite.com
ServerAlias *
DocumentRoot /var/www/html/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ https://mynewwebsite.com/$1 [L,R=301]
</IfModule>
</VirtualHost>
</IfModule>
This prevent to get the default page even if the user try to make a request with a FQDN different from your configuration.
Important!: You have to respect the order of configuration.
Salvo.

The problem solved when I replaced ServerName with my IP instead of my server FQDN. I assume this method only works, if you have added the server and domain in /etc/hosts, which I have already added.
<VirtualHost *:80>
ServerAdmin admin#mynewwebsite.com
ServerName xx.xx.xx.xx <------------------- IP
ServerAlias www.mynewwebsite.com
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mynewwebsite.com [OR]
RewriteCond %{SERVER_NAME} =www.mynewwebsite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin#mynewwebsite.com
ServerName mynewwebsite.com <------------------- Domain
ServerAlias www.mynewwebsite.com
DocumentRoot /var/www/html/wordpress
ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mynewwebsite.com [OR]
RewriteCond %{SERVER_NAME} =www.mynewwebsite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Related

How can I redirect traffic staging site without combining the domain prefix and suffix?

I've got a question about apache configs or it may be the .httaccess file. I have staging site : staging.witnesstolove.org with https enabled. when I try to go to the site, I'm redirected to stagingwitnesstolove.org and I'm not sure why?
My apache config seems pretty basic:
<VirtualHost *:80>
ServerAdmin accounts#1905newmedia.com
DocumentRoot /home/ubuntu/staging/witnesstolove/current/site/web/
ServerName staging.witnesstolove.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/staging/witnesstolove/current/site/web/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =staging.witnesstolove.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I see in devtools that i'm getting a 301 redirect and it says it's being redirected by wordpress.
If I look at the .htaccess file for the site, it's like this:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Based on the response headers I'm getting back, it sounds like wordpress is redirecting the traffic to the other domain?
Here's the HTTPS config for the site in Apache:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin accounts#1905newmedia.com
DocumentRoot /home/ubuntu/staging/witnesstolove/current/site/web/
ServerName staging.witnesstolove.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/staging/witnesstolove/current/site/web/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
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} =staging.witnesstolove.org
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/staging.witnesstolove.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/staging.witnesstolove.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Using apache proxypass to redirect to /blog (wordpress)

I'm running a ReactJS app at https://www.hackachieve.com with the following structure:
Port 80/443: Front-end - ReactJS SPA
Port: 8000 - Back-end - django rest framework API
I've installed wordpress properly at /var/www/html and I'm trying to redirect the user from /blog to it.
The issue is that my proxy pass is not working. What happens when the user tries to reach https://www.hackachieve.com/blog is that its redirected back to the main page /. I've found a 301 redirect by inspecting the devtools.
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName hackachieve.com
ServerAlias www.hackachieve.com
ServerAdmin joaopaulofurtado#live.com
DocumentRoot /var/www/hackachieve-frontend
ProxyPreserveHost On
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
RewriteEngine on
RewriteCond %{SERVER_NAME} =hackachieve.com [OR]
RewriteCond %{SERVER_NAME} =www.hackachieve.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName hackachieve.com
ServerAlias www.hackachieve.com
ServerAdmin joaopaulofurtado#live.com
DocumentRoot /var/www/hackachieve-frontend
ProxyPreserveHost On
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
ErrorLog ${APACHE_LOG_DIR}/error-frontend.log
CustomLog ${APACHE_LOG_DIR}/access-frontend.log combined
RewriteEngine off
RewriteCond %{SERVER_NAME} =www.hackachieve.com [OR]
RewriteCond %{SERVER_NAME} =hackachieve.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hackachieve.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hackachieve.com/privkey.pem
</VirtualHost>
<VirtualHost *:8000>
ServerName hackachieve.com
ServerAlias www.hackachieve.com
ServerAdmin joaopaulofurtado#live.com
DocumentRoot /var/www/hackachieve-backend
ErrorLog ${APACHE_LOG_DIR}/error-backend.log
CustomLog ${APACHE_LOG_DIR}/access-backend.log combined
Alias /static /var/www/hackachieve-backend/static
<Directory /var/www/hackachieve-backend/static>
Require all granted
</Directory>
<Directory /var/www/hackachieve-backend/hackachieve>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPassAuthorization On
WSGIDaemonProcess hackachieve python-home=/var/www/hackachieve-backend/venv python-path=/var/www/hackachieve-backend
WSGIProcessGroup hackachieve
WSGIScriptAlias / /var/www/hackachieve-backend/hackachieve/wsgi.py
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hackachieve.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hackachieve.com/privkey.pem
</VirtualHost>
I expect that when the user reaches https://www.hackachieve.com/blog he gets redirected to my Wordpress blog.
I don't want to ReactJS to handle this route

Symfony application shows directory listing when using https

Normal VHOST:
<VirtualHost *:80>
DocumentRoot /var/www/html/app/current/web
ServerAdmin me#app.foobar
ServerName app.foobar
ServerAlias www.app.foobar
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options Indexes FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !HTTP/1.1$
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
</IfModule>
</VirtualHost>
HTTPS VHOST:
<VirtualHost *:443>
DocumentRoot /var/www/html/app/current/web
ServerName app.foobar
ServerAdmin me#app.foobar
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite>
Options Indexes FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !HTTP/1.1$
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
SSLEngine on
SSLProtocol +TLSv1 +TLSv1.1 +TLSv1.2
SSLCertificateFile /ssl/app.foobar/app.foobar.crt
SSLCertificateKeyFile /ssl/app.foobar/app.foobar.key
SSLCACertificateFile /ssl/app.foobar/app.foobar.ca-bundle
</VirtualHost>
As the title says when I use regular port 80 the urls are rewritten to use app.php but when I try to use https port 443 I get a directory listing instead. How do I get the url rewritten with https like it does with http?
I solved this by using the FallbackResource directive available in Apache 2.2.16+.
In the official Symfony docs this directive is not used to remain backwards compatible with older version of Apache. Discussion is here.
Code example:
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
FallbackResource /app.php
</IfModule>
</Directory>

URL redirection and rewriting at the same time

I have sajjanlamichhane.com website hosted on Amazon EC2. But my actual site is inside sajjanlamichhane.com/wp/. So, I redirect URL but I want to mask the redirect URL. How do I do this?
I have this below
Listen 80
<VirtualHost *:80>
ServerAdmin root#sajjanlamichhane.com
ServerName sajjanlamichhane.com
ServerAlias www.sajjanlamichhane.com
DocumentRoot /var/www/sajjanlamichhane.com/
ErrorLog /var/www/sajjanlamichhane.com/logs/error.log
CustomLog /var/www/sajjanlamichhane.com/logs/access.log combined
Redirect http://sajjanlamichhane.com "http://sajjanlamichhane.com/wp/
RewriteEngine on
RewriteRule ^/$ http://sajjanlamichhane.com/wp/index.php [R=301,NC,L]
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^wp/ /wp%http://sajjanlamichhane.com [L]
</VirtualHost>
This is how you solve. Further links to read:
http://www.willmaster.com/library/web-development/url-masking.php
Listen 80
<VirtualHost *:80>
ServerAdmin root#sajjanlamichhane.com
ServerName sajjanlamichhane.com
ServerAlias www.sajjanlamichhane.com
DocumentRoot /var/www/sajjanlamichhane.com/
ErrorLog /var/www/sajjanlamichhane.com/logs/error.log
CustomLog /var/www/sajjanlamichhane.com/logs/access.log combined
Redirect http://sajjanlamichhane.com "http://sajjanlamichhane.com/wp/
RewriteEngine on
RewriteCond http://sajjanlamichhane.com/wp/index.php http://sajjanlamichhane.com/wp/index.php$
RewriteRule .* /wp/index.php [L]
</VirtualHost>

wordpress urls not working with apache virtualhost

I have setup a apache virtual host using the below script -
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName nvoids.cu.cc
ServerAlias www.nvoids.cu.cc
DocumentRoot /var/www/html/blog
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now this is working fine at this url - http://nvoids.cu.cc
which serves the pages from /var/www/html/blog
But the wordpress permalinks are not working for example
http://nvoids.cu.cc/hello/ throws a 404 page.
For that i tried changing the .htaccess file as follows -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . blog/index.php [L]
</IfModule>
But its not working.
You need
AllowOverride All
In your VirtualHost directive to enable use of .htaccess files.
Is the following module enabled in your php.ini which should be uncommented? Do you see any error in your apache error log file?
LoadModule rewrite_module
As answered by Daniel the virtual server conf should be -
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName nvoids.cu.cc
ServerAlias www.nvoids.cu.cc
DocumentRoot /var/www/html/blog
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html/blog/>
AllowOverride All
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The .htaccess file as it is modified by wordpress -
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources