Redirect non-existent dynamic virtual host folders with Apache - dynamic

Is it possible to redirect to a certain URL if the directory for the virtual host doesn't exist? I have a VirtualHost setup below that lets me host a domain just by creating the folder, and pointing DNS to my server.
Here is my configuration for the VirtualHost
<VirtualHost *:80>
ServerAdmin webmaster#localhost
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/ [R=301,L]
VirtualDocumentRoot /var/www/%0
<Directory /var/www/%0>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

You can manage it using rewriting rules.
Just after you rewriting rule to skip www :
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteCond /var/www/%1 !-d
# I think just /var/www/%{HTTP_HOST} works :|
RewriteRule .* http://example.com/nonexistant.php/$0 [L,R]

Related

laravel5.1 laravel in subfolder (routes not working) results in 404 apache error

My current setup on my production server is like the following:
there are two folders in
var/www/html
These two folders are named:
xyz
doge
Now I would like to place all requests to IP/doge into the doge folder.
This seems to work, but only the index page is working. Using the already created routes like for example:
ip/doge/login
will result in a 404 error page of apache.
Here's my apache config:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/xyz/public
Alias "/doge" "/var/www/html/doge/public"
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html/xyz/public/>
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Any idea where's my fault? I haven't changed the .htaccess in the public folder of each laravel project. This .htaccess looks like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
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]
</IfModule>

Apache ignores .htaccess file

I have a debian apache server. For some reason apache ignores my .htacces file. Any ideas why this is happening?
Here is my /etc/apache2/sites-available/default file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride none
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Here is my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I have also enabled apache rewrite mod.
Panama Jack thank you so much,
Based on your answer i find out the solution i was looking for. I added at the top of my .htaccess this line
Options +FollowSymLinks -MultiViews
that i found here
RewriteEngine On .htaccess not working
Thanks so much again!

apache2 mod_rewrite based on language then proxy

I am trying to redirect a user that visits www.server.com to the browser's preferred language setting.
When a user types in www.server.com/<lang>, /es for spanish in the case below, they are correctly proxied to the smartling translation servers. However, when a user has their language preference set in their browser, they experience a redirect loop.
I have the following config:
<VirtualHost *:8008>
ServerName www.server.com
ServerAlias www
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^/$ es/ [R=301,L]
<Proxy http://server.sl.smartling.com/*>
Allow from all
</Proxy>
<LocationMatch "/es">
ProxyPass http://server.sl.smartling.com/es
ProxyPassReverse http://server.sl.smartling.com/es
Header add Host "www.server.com"
RequestHeader set Host "www.server.com"
</LocationMatch>
DocumentRoot /www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/www-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/www-access.log combined
</VirtualHost>
I've tried stopping the loop with additional RewriteCond but can't seem to get it right. Any suggestions appreciated.
Thanks
You'll need to add another condition to negate the locale condition. The locale condition will always be true if it is set in the user's browser. Try adding a condition like this
RewriteCond %{REQUEST_URI} !^/es

Subdomains Always Redirect To Main Domain - Apache2 VirtualHosts & Debain

I want to set up a main site, and a sub-domain for development, using apache2 VirtualHosts. This is what my site.conf virtual host file looks like:
<VirtualHost *:80>
ServerName dev.mysite.com
DocumentRoot /var/www/site/subdomain
<Directory /var/www/site/subdomain >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/sub.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com
DocumentRoot /var/www/site/main
<Directory /var/www/site/main >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The problem is that every time I visit dev.mysite.com, I just get mysite.com.
There's no htaccess for the main site, and the .htaccess for the sub-domain looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I've tried all the other solutions I could find (using literal IPs, re-ordering things, using ports instead of named hosts)... I'm totally stuck now. The site is enabled of course. Anyone got any ideas?
Wordpress Was The Problem
Or rather, me not knowing that I had to configure the Wordpress site url to be the subdomain, otherwise it would just redirect to the domain given...
So the solution: open the wp_options table and edit the site_url to include the subdomain.

Apache ErrorDocument issue

i am trying to make my own 403 page but my apache doesn work correct.
I use
ErrorDocument 403
but it shows the same. This is my .htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Here is my host settings:
<VirtualHost 127.0.1.15:80>
ServerAdmin webmaster#localhost
ServerName turik.loc
ServerAlias *.turik.loc
DocumentRoot /home/golars/proj/turik/
<Directory />
Options FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory /home/golars/proj/turik/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/t4base_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
To 403 error i am going to turik.loc/img/hotels/ -where i dont have permissions to read&write. I would be graceful for a help.