How to domain redirect in apache? - apache

I'm trying to redirect domain-a to domain-b. So when the user types in domain-a, it will lead them to domain-b. I tried using an HTACCESS file and I'm not having any luck. So here is what I put in the server config file.
<VirtualHost *:80>
ServerAdmin support#domain-b.com
ServerName domain-a.com
ServerAlias www.domain-a.com
DocumentRoot /var/www/domain-a.com/public_html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
# Testing the rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-a\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteRule ^/?$ "http\:\/\/domain-b.com\.com\/" [R=301,L]
</VirtualHost>

Related

Vuejs App still get 404 page after edit .htaccess

I have a Vuejs App deployed at apache2 server in digitalocean , when you hit the url it forwards you to login page which is working fine and then automatic navigate me to dashboard page that have cards when I click on one of cards I get 404 page even after I edit my .htaccess file in dist
NOTE the card route is domain.com/build
I tried .htaccess configuration from docs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
and here is my virtual host configuration
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin email
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html/app/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass /api/ http://localhost:8000/
ProxyPassReverse /api/ http://localhost:8000/
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
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Nothing wrong with virtual host file, the problem was in apache2.conf file in /etc/apache2. what I need is to change this part from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to :
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
you need to change the AllowOverride attribute.

Incorrect SSL-configuration for BookStack

after trying unsuccessfully to set up BookStack with nginx, I switched to Apache.
However, I have similar difficulties setting up the subfolder.
BookStack should be available at https://domain.name/bookstack.
The location of BookStack is /var/www/bookstack/... .
I have also added the domain in the .env.
I just figured out, that the configuration for HTTP works like expected.
<VirtualHost *:80>
ServerName domain.name
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# BookStack Configuration
Alias "/bookstack" "/var/www/bookstack/public"
<Directory "/var/www/bookstack/public">
Options FollowSymlinks
AllowOverride none
Require all granted
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
<Directory "/var/www/bookstack">
AllowOverride None
Require all denied
</Directory>
# End BookStack Configuration
</VirtualHost>
But when I put in the config for HTTPS (I created a certificate with certbot/letsencrypt), I get the login page without styles. As I log in, it takes me to HTTP-domain with error 404 Not found.
Here's my current SSL config:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName domainname
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# BookStack Configuration
Alias "/bookstack" "/var/www/bookstack/public"
<Directory "/var/www/bookstack/public">
Options FollowSymlinks
AllowOverride none
Require all granted
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
<Directory "/var/www/bookstack">
AllowOverride None
Require all denied
</Directory>
# End BookStack Configuration
SSLEngine on
SSLCertificateFile /path/to/certificate/file
SSLCertificateKeyFile /path/to/certificate/key/file
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I don't get the problem with this.

Server not reading htaccess file and redirect not working

My site is not reading the htaccess file. Even if I add Deny from all at the top, the site loads fine.
Permission is 644. I have a number of sites running on the server so Apache is set up correctly.
This is my virtualhost config:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
This is what I'd added to .htaccess file to redirect www to non-www:
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
I tried adding it to virtualhost as well but it's still not redirecting.
After issuing an SSL Certificate with Let's Encrypt I had noticed it had created a duplicate conf file for port 443:
/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost 12.34.56.78:443>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Prior to the LE, the original example.com.conf didn't have the AllowOverride All directive. So I assume what has happened is the contents were duplicated and then I only changed the original one.

How can i set htaccess file for virtual host?

I created a virtual host with this code :
<VirtualHost *:80>
ServerAdmin admin#127.0.0.1
ServerName site.ws
ServerAlias www.site.ws
DocumentRoot /home/me/Projects/website/build
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/me/Projects/website/build>
Allow from all
Satisfy any
</Directory>
</VirtualHost>
and I created a .htaccess file in my /build directory with this code :
RewriteEngine On
RewriteRule ^(.*)$ $1.html [R,NC]
Consider my mod_rewrite is active in apache2, but I can't open pages with /filename
e.g site.ws/about
It shows error :
The requested URL /about was not found on this server.
I try this with Apache2 2.4.27 in win:
First enable vhost in httpd.conf file.
vhost:
<VirtualHost *:80>
ServerName site.ws
DocumentRoot /home/me/Projects/website/build
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/me/Projects/website/build>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
.htaccess:
###START MOD_REWRITE
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#REMOVE .html EXTENSION
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
###END MOD_REWRITE
i solved this problem with :
RewriteEngine On
RewriteRule ^([a-zA-Z-]+)$ $1.html [L,NC]

Redirect Error: Wordpress + Let's Encrypt (Certbot) + SSL only + non-www

Note: See the updates at the end of this post. For the final (working) conf-files see update 4 at the end of this post or the post which I marked as the solution.
I badly configured my apache conf-files and now I'm getting a redirect error (ERR_TOO_MANY_REDIRECTS). I want to redirect everything to HTTPS (non-www). I already tried to add this to the wp-config.php regarding this tips, but that didn't solve the problem:
define('WP_HOME','http://d0main.xyz');
define('WP_SITEURL','http://d0main.xyz');
and I tried to add
define('WP_HOME','https://d0main.xyz');
define('WP_SITEURL','https://d0main.xyz');
Here are my Apache files:
d0main.xyz.conf
<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
d0main.xyz-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin d0main#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/d0main.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/d0main.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>
</IfModule>
<IfModule mod_rewrite.c>
Update 1: I solved the redirection error by removing Redirect permanent / https://d0main.xyz in my d0main.xyz-le-ssl.conf, but now there is a / missing at the end of the URL. Graphic URL's for example are now https://d0main.xyzwp-content/image.jpg
Update 2: This is getting more weird. I changed the line Redirect permanent / https://d0main.xyz to Redirect permanent / https://d0main.xyz\/ in my d0main.xyz.conf. Now some images have two slashes (and work) https://d0main.xyz//wp-content/uploads/2016/10/logo-5.png, while other images still have no slash in their URL: https://d0main.xyzwp-content/uploads/2016/10/image2.png
Update 3: I forgot to post my .htaccess
# 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
Update 4: My final (working) conf-files:
d0main.xyz.conf
<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz/
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
d0main.xyz-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName delegatex.xyz
ServerAlias www.delegatex.xyz
ServerAdmin delegatexyz#gmail.com
DocumentRoot /var/www/html
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/delegatex.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/delegatex.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
<IfModule mod_rewrite.c>
You do not need to escape the forward slash, so instead of Redirect permanent / https://d0main.xyz\/ you should just have Redirect permanent / https://d0main.xyz/. But when redirecting (and ProxyPassing) you should always match trailing slashes.
Instead of the following lines.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
You should have a SSL vhost for www.d0main.xyz and jsut do Redirect / https://d0main.xyz/
If you are still getting the double slashes it looks like the reason must be elsewhere. DO you have any htaccess files?