How to set up my virtualhost on apache to render correct links to css and images - apache

I've installed Scriptcase manually to my apache webserver running ubuntu 20.04 and PHP 7.3 and it is loading the page. BUT all sublinks are not working as images/css files are not rendered correctly (it can't find them)
[Image showing the page with broken links][1]
[1]: https://i.stack.imgur.com/8VodU.png
Example:
Image links are read like ... https://devel/conf/scriptcase/img/btnnew/crystal/sc_ico.png
It should be: https://wmqms-dev.wismatix.net/devel/conf/scriptcase/img/btnnew/crystal/sc_ico.png
My virtualhost file is like the below:
<VirtualHost *:80>
ServerName wmqms-dev.wismatix.net
VirtualDocumentRoot /var/www/wmqms-dev
Redirect / https://wmqms-dev.wismatix.net
# RewriteEngine on
# RewriteCond %{SERVER_NAME} =wismatix.net [OR]
# RewriteCond %{SERVER_NAME} =wmqms-dev.wismatix.net
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
UseCanonicalName Off
<VirtualHost *:443>
ServerName wmqms-dev.wismatix.net
VirtualDocumentRoot /var/www/wmqms-dev
<Directory /var/www/wmqms-dev/>
Options FollowSymLinks
AllowOverride None
</Directory>
# <IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{HTTP_HOST} .
# RewriteRule ^/([0-9]+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
# RewriteRule ^index\.php$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
# </IfModule>
SSLEngine On
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/wmqms-test.wismatix.net-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/wmqms-test.wismatix.net-0001/privkey.pem
</VirtualHost>
I've been fiddling with this for a while and tried searching thru the internet including 'StackOverflow' and read several articles but either I don't understand them ;-) or ?
Can anyone point me in the right direction ... I'm guessing ReWrite or ?
Thanks in advance ...
Jonatan

Related

.htaccess multiple domains

I have one host for two domain. The first domain and its rules (aaaaa.com) are working.
When I try to append rules of second one (bbbbb.com) send an Internal Error 500 message.
The goal is bbbbb.com should reply the content of bbbbb directory. Please help..
Options -Indexes
<IfModule mod_rewrite.c>
ErrorDocument 404 /errors/error404.html
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www\.)(?!aaaaa\.com)[^\.]+)\.
RewriteRule ^(.*)$ aaaaa/index.html?p=%1 [L]
RewriteCond %{HTTP_HOST} ^bbbbb\.com [NC]
RewriteRule ^(.*)$ bbbbb/%1 [R,L]
</IfModule>
If you have access to the apache configuration, I recommend you to use Virtual Hosts to achieve your purpose.
You should include this configuration in your /etc/apache2.conf file:
<VirtualHost *:80>
DocumentRoot "/www/aaaaa"
ServerName aaaaa.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/bbbbb"
ServerName bbbbb.com
</VirtualHost>
Supposing that your web directories are stored in /www.
You can find the documentation here:
https://httpd.apache.org/docs/current/vhosts/

Redirect with htaccess on apache

I am trying to create a virtualhost and allow a htaccess to redirect (it is for a laravel project).
I am my environment is: ubuntu16.04 apache2.4.18 php7.0.4
And here is my /etc/apache2/sites-available/sub.domain.com.conf
<VirtualHost *:80>
ServerName eatec.es
ServerAlias webs.eatec.es
ServerAdmin eloy#eatec.es
DocumentRoot /var/www/html/sub.domain.com/public
<Directory "/var/html/sub.domain.com/public">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Things that I tried (and remember).
1. The line of Directory I tried removing the quotes.
2. The line of Directory I used too this (removing the subdomain sub -with and without the quotes-).
3. I move the Directory block from this file to /etc/apache2/apache2.conf (I paste the last of this type of blocks).
4. I change the AllowOverride option on the block (on the apache2.conf -with my Directory block in apache.conf but too on sub.domain.com.conf-).
5. There was more micro changes, sure... but I don´t remember.
One more thing I tried was a2enmod rewrite but it doesn´t work (though I see mod_rewrite on the loaded modules if I make a phpinfo()).
This is my .htaccess content:
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
It is the default htaccess file on a laravel project. It is supposed to redirect the index.php on the same folder. My folder structure is the next:
/var/www/html/sub.domain.com-------------> All The Project
/var/www/html/sub.domain.com/public---> htaccess and index.php
Any suggestion?? Thanks.

Apache: difference virtualhost and htaccess

I set up a private server (raspberry) with apache.
the config file for the website:
<VirtualHost *:80>
ServerName me.example.com
ServerAlias me.example.com
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC]
DocumentRoot /var/www/my/root
<Directory /var/www/my/root>
Options FollowSymLinks
Options -Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1.) If I request now a "subpage" like /impressum.php. It will show me the impressum, instead of rewriting to the index file. Why?
2.) Now I copied the part of the rewrite engine into a .htaccess file and it doesn't work anymore. Where is the difference between these two files? What do I need to change, so it will work?
My .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC,L,QSA]
Thanks in advance
the files were missing ... so it redirected to the home.
It works better then me

Redirect all to https://www.example.com

OK, so I know that nearly every variety of this question has been asked and answered on this forum, and many others, but I just can't seem to get it right. My hope is that if I provide enough specifics, including the process I'm following, that someone will be able to spot my error and correct me. Here goes!
The Specifics
Drupal multi-site
Apache web server
Sites provisioned by Aegir
What I want to accomplish vs what I have accomplished
I want to direct all http and https traffic to https://www.example.com. The following examples marked with a + have been accomplished; the one marked with - eludes me:
+ http://example.com -> https://www.example.com
+ http://www.example.com -> https://www.example.com
+ https://www.example.com -> https://www.example.com #redundant, but at least we know it works
- https://example.com -> https://www.example.com
What I'm doing
Each site has a vhost file generated by Aegir, and each of these vhost files contains two VirtualHost directives:
<VirtualHost 0.0.0.0:443>
<VirtualHost *:80>
I've modified example.com's vhost file to include the following code under the <VirtualHost *:80> directive, which has successfully achieved the three working examples above:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
The Problem
What I can't seem to figure out is how to enact the https://example.com -> https://www.example.com rewrite. I've tried many examples found on the Internet under both the <VirtualHost 0.0.0.0:443> and <VirtualHost *:80> directives with no luck.
Also, I'm reloading Apache and clearing my browser's cache after each edit of the vhost file. Any insight, guidance or correction would be much appreciated.
Thanks to all!
Edit:
Below is a redacted version of my vhost file:
<VirtualHost 0.0.0.0:443>
DocumentRoot /var/www/drupal
ServerName example
SetEnv db_type mysql
SetEnv db_name example
SetEnv db_user example
SetEnv db_passwd 1234567
SetEnv db_host somedb
SetEnv db_port 1234
# Enable SSL handling.
SSLEngine on
SSLCertificateFile /ssl.d/example/example.crt
SSLCertificateKeyFile /ssl.d/example/example.key
SSLCertificateChainFile /ssl.d/example/example_chain.crt
ServerAlias example.com
ServerAlias www.example.com
<IfModule mod_rewrite.c>
RewriteEngine on
# this isn't working; neither has anything else that I've tried here.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Extra configuration from modules:
# Error handler for Drupal > 4.6.7
<Directory "/var/www/drupal/sites/example/files">
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
# Prevent direct reading of files in the private dir.
# This is for Drupal7 compatibility, which would normally drop
# a .htaccess in those directories, but we explicitly ignore those
<Directory "/var/www/drupal/sites/example/private/" >
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Deny from all
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/drupal
ServerName example
SetEnv db_type mysql
SetEnv db_name example
SetEnv db_user example
SetEnv db_passwd 1234567
SetEnv db_host somedb
SetEnv db_port 1234
ServerAlias example.com
ServerAlias www.example.com
<IfModule mod_rewrite.c>
# these rules work for everything except:
# https://example.com -> https://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
# Extra configuration from modules:
# Error handler for Drupal > 4.6.7
<Directory "/var/www/drupal/sites/example/files">
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
# Prevent direct reading of files in the private dir.
# This is for Drupal7 compatibility, which would normally drop
# a .htaccess in those directories, but we explicitly ignore those
<Directory "/var/www/drupal/sites/example/private/" >
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Deny from all
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
</VirtualHost>
OK now I get it with the content of your virtualhost.
This is not valid:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Actually you can't use %{HTTP_HOST} or %{REQUEST_URI} in the RewriteRule part
That's why it's redirecting you to a non-existent host, hence the connection refused error.
You should use this rule in the ssl virtualhost:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
And this one in the non ssl one:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com$1 [R=301,L]
HTH

Phalcon keeps using index.phtml for different Controllers

I'm new to Phalcon and I like the framework but I have problems with links. I use phalcon version 0.6.1 and XAMPP 1.8.1 that has vhosts set to xampp/htdocs/test where my phalcon test is.
I have been following the tutorial and came to a problem. When I use links to load other controllers the adress bar shows the correct path but as far as I can see the index.phtml get loaded every time. I uploaded the files here so you can see for yourselves.
It doesn't matter if I use Tag::LinkTo() or since it doesn't change.
edit:
I followed the instructions, removed the .htaccess files from the /test and /test/public directories to move the to httpd.conf. At the end of it I added
<Directory "C:/xampp/htdocs/www/test">
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</Directory>
<Directory "C:/xampp/htdocs/www/test/public">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</Directory>
and modified my httpd-vhosts.conf like this
ServerAdmin admin#example.host
DocumentRoot "C:/xampp/htdocs/test/public"
DirectoryIndex index.php
ServerName example.host
ServerAlias www.example.host
<Directory "C:/xampp/htdocs/test/public">
Options All
AllowOverride All
Allow from all
</Directory>
The page loads but the absolute links like /public/css/bootstrap.min.css don't work and when I click the link it gives me an error 404 and says Object not found. I modified the files like this:
<Directory "C:/xampp/htdocs/test">
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</Directory>
<Directory "C:/xampp/htdocs/test/public">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</Directory>
and
ServerAdmin admin#example.host
DocumentRoot "C:/xampp/htdocs/test"
DirectoryIndex index.php
ServerName example.host
ServerAlias www.example.host
<Directory "C:/xampp/htdocs/test">
Options All
AllowOverride All
Allow from all
</Directory>
But that brings me to my original problem that when I click the link it loads index.phtml again even though it says localhost:8005/sample in the URL.
It looks like your Apache configuration is not allowing reading the .htaccess file:
<VirtualHost *:80>
ServerAdmin admin#localhost
ServerName phalcon.test
DocumentRoot /srv/htdocs/sites/test
<Directory "/srv/htdocs/sites/test">
AllowOverride All
Options All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Also, at the top of your public/index.php check that $_GET['_url'] has the URI passed to your browser