Proper way to set up apache2 default config and htaccess in project directory - apache

Dear people,
My goal is to set up the apache2's 000-default.conf and the .htaccess in a specific way, so the user will always be redirected to a specific php file (index.php for example) if they (the user) wants to access the web app, which I am building.
Examples :
example-app.local
example-app.local/hello-world
example-app.local/admin.php
example-app-local/../../../../my-passwords.txt
All of them should point to index.php. I followed several tutorials, but I guess I need a specific tutorial for me, so that I can fully understand the concepts of apache2 and its configurations. Feel free to judge me - I am alright with criticism.
What I have in my files is the following
1. The 000-default.conf file which is located in /etc/apache2/sites-available/
<VirtualHost *:80>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
...and loads of comments
2. The example-app.local.conf file which is located in /etc/apache2/sites-available/
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example-app.local
ServerAlias www.example-app.local
DocumentRoot "/var/www/example-app"
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3. The .htaccess file which is located in /var/www/example-app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,R]
What I get is the following : "Apache default conf is like a god of the configurations and you can state specifically that you can AllowRedirect which will magically trigger the functionality of htaccess and there you need the even more magical syntax to make every request from the user point ONLY to the index.php" Is this correct?
What is working Apache is working. I am enabling and disabling configs via
sudo a2ensite 000-default.conf
sudo a2dissite 000-default.conf
and then I restart and check that everything is fine via
sudo service apache2 restart
sudo service apache2 status
the status states active (running).
The content of /etc/hosts is also edited in a correct way, because I can actually see the content of the index.html file when I try to point my browser to the example-app.local page. I would also like to point out that I have php installed correctly - I tested it via the phpinfo function/method.
I tried several tutorials and none of them helped me. I believe I am missing something important and I am too 'blind' to see it, so I need someone to 'open' my eyes, so please - help me.
Best regards,
Yet another confused startup developer,
Bobkoo

Try tis one:
After RewriteEngine On try RewriteBase / and lose the slash before index.php
BTW you working on Windows... so the DocumentRoot "/var/www/example-app" should be something like C:\xampp\htdocs\project-folder

Related

Serve content from first domain's subdirectory on another domain using Apache

I have a website, say domain1.example, deployed on an Amazon EC2 machine with apache2.
I want to point domain2.example to domain1.example/path.
Basically like this:
domain2.example -> domain1.example/path
domain2.example/one -> domain1.example/path/one
domain2.example/two/page -> domain1.example/path/two/page
(Without changing the URL in the browser's address bar)
I have tried these methods:
Adding a rewrite rule to /etc/apache2/sites-available/000-default.conf
...
<VirtualHost *:80>
ServerName "domain2.example"
ServerAdmin email#domain1.example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteRule ^(.*)$ https://domain1.example/path/$1 [P]
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Adding a rewrite rule to .htaccess in the project folder
RewriteCond %{HTTP_HOST} ^domain2\.example [NC]
RewriteRule ^(.*)$ https://domain1.example/path/$1 [P]
Neither of them works. Both domains are serving the root folder. Am I doing something wrong?
I would implement this using separate virtual hosts for the two domains that have different document roots.
<VirtualHost *:80>
ServerName "domain1.example"
DocumentRoot /var/www/html
...
</VirtualHost>
<VirtualHost *:80>
ServerName "domain2.example"
DocumentRoot /var/www/html/path
...
</VirtualHost>
While this will accomplish your goal, I would not recommend setting up websites like this.
If both sites have .htaccess files they can conflict with each other. You may see different behavior when accessing the content on the different domains because of which rules apply to in the two scenarios.
Search engines prefer to find content on just one "canonical" URL. When you serve the same content on multiple domains it can confuse search engines and hurt your SEO.

Lamp server not showing file without extension

I am getting a weird issue i created a vhost in lamp stack. The problem is that when I open the vhost site www.domain.com the home page loads without any issue.
But when I use the nav bar to open another page I get a "404 NOT FOUND" i.e www.domain.com/about
But as soon as I put the extension of the file manually the page loads.
www.domain.com/about.php
How can I solve this issue. I am using .htaccess to hide the extension of the file.
Note:
1) All the other local file running properly i.e. I have a wordpress site that works fine (This means the mysql db is not causing the error)
2) The vhost is set properly bcus the terminal did not show any error when the vhost was enabled.
But the vhost in wamp does not give me any kind of issue in my windows 8.1 pro.
Edit :
Code in .htaccess to hide extension this works in my windows pc without any problem.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
EDIT -2
<VirtualHost *:80>
<Directory /var/www/stab-website>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
DocumentRoot "/var/www/stab-website"
ServerName stab-site.com
ServerAlias www.stab-site.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Still did not worked.
You need to enable the MultiViews options:
<VirtualHost *:80>
<Directory /var/www/htdocs>
Options Indexes FollowSymLinks MultiViews
</Directory>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/var/www/htdocs"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
</VirtualHost>
This can either be done in your httpd.conf file, in the configuration file for your virtual host or in a .htaccess file.
You should also make sure so that the mod_negotiation module is enabled. This can be done by running:
sudo a2enmod negotiation
You also might need to verify that your virtual host configuration is fine, this can be done from the terminal using:
apache2ctl -t
After this you need to restart your server:
sudo service apache2 restart
From the documentation:
The effect of MultiViews is as follows: if the server receives a
request for /some/dir/foo, if /some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the server reads the directory
looking for files named foo.*, and effectively fakes up a type map
which names all those files, assigning them the same media types and
content-encodings it would have if the client had asked for one of
them by name. It then chooses the best match to the client's
requirements.
Thanks #Cyclone for your help
This post helped me to fix this issue
https://askubuntu.com/questions/233046/how-to-give-my-user-permission-to-add-edit-files-on-local-apache-server
The post above help to run Apache server as the logged in user.
To give rights to use .htaccess --
First, you should ensure that your username is included in www-data group. If not, you can add your username as www-data group
sudo adduser $USER www-data
After that, you should change the ownership of /var/www to your username
sudo chown $USER:www-data -R /var/wwws
And also don't forget to add this code in the apache2.conf
<Directory /var/www/ProjectRootDirectory>
AllowOverride All
</Directory>

keep name domain htaccess

I have a little problem. I have a hosting with piensasolutions and two domains. The first domain controls the hosting and the second use a domain parking.
Let a.com be the first domain and b.com be the second domain.
My domain parking for b.com allows me to redirect from b.com to a.com/folder, (where folder contains the web page where I want to point with b.com).
The problem is that I want to show b.com URL instead of a.com/folder URL, I am not sure if the problem is from piensasolutions because I tried several .htaccess files but I was not able to change the URL of the web browser. This was my last test which supposes to keep the original URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?a.com/folder/$
RewriteRule ^(.*)$ /a.com/folder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?b.com$
RewriteRule ^(.*)$ /b.com/$1 [L]
For additional information, I put the .htaccess file inside the a.com/folder. I am not sure about the redirection of piensasolutions.
If your are able to help me, I will be really grateful, and also if you have dedicated a couple of minutes even if you have not find the anwser.
Thank you!
I think that the best way is usig sites-avaiable.
First of all,
$ vi /etc/host
and copy the next line:
127.0.0.1 a
127.0.0.1 b
Then, create two new sites:
$ touch /etc/apache2/sites-avaiable/a
$ touch /etc/apache2/sites-avaiable/b
and edit them
$ vi /etc/apache2/sites-avaiables/a
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName a
DocumentRoot /your_path_to_server/a/folder
<Directory /your_path_to_server/a/folder/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and
$ vi /etc/apache2/sites-avaiables/b
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName b
DocumentRoot /your_path_to_server/b
<Directory /your_path_to_server/b/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and that's all. This should work.

CF file not found with Apache virtual hosts

I have Apache 2.2 configured to run with Coldfusion 9.2, both on the same Centos 6.2. The general config of DocumentRoot in Apache is
DocumentRoot "/var/www/html"
Up until now Apache handed all calls to cfcs and cfms over to Coldfusion. I then created a virtual host
<VirtualHost 192.168.123.207:80>
ServerName myserver.server.private
ServerAlias www.myserver.server.private
ServerAlias webmail.myserver.server.private
ServerAlias admin.myserver.server.private
DocumentRoot /home/myserver/public_html
ErrorLog /var/log/virtualmin/myserver.server.private_error_log
CustomLog /var/log/virtualmin/myserver.server.private_access_log combined
ScriptAlias /cgi-bin/ /home/myserver/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/myserver/public_html>
Options +Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch
allow from all
AllowOverride All
Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
</Directory>
<Directory /home/myserver/cgi-bin>
allow from all
AllowOverride All
Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.myserver.server.private
RewriteRule ^(.*) http://myserver.server.private:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.myserver.server.private
RewriteRule ^(.*) http://myserver.server.private:10000/ [R]
</VirtualHost>
When I open a html file on http://myserver.server.private/hello.html, the correct file within the virtual host document root (/home/myserver/public_html) gets served.
Calling a cfm fails though. Apache passes the request on correctly but then a Coldfusion error message shows up:
File not found: /hello.cfm
It seems that Coldfusion is getting the file name, prefixes a wrong directory path to it and fails loading it from the disk.
I scanned all Coldfusion files for a wrong document root but could not find anything.
EDIT
It turns out that Coldfusion looks into it's own webroot directory at
/opt/coldfusion/wwwroot
I placed a file with the same name but different content there and that one gets served, although there is a) a virtual host and b) the general DocumentRoot is set to /var/www/html.
It turns out that the coldfusion user (cfusion in my case) needs to be in the group of the virtual host user.
usermod -a -G newuser cfusion
After a restart of coldfusion, all works.

Apache - Can no longer use mod_rewrite after adding virtual directory

My apache server is set up with a very basic configuration. I used to serve just one website from apache, let's call it example.com. Within the httpd.conf file, I had some code to force the website to always show www in the url.
I recently added a subdomain for the site, blog.example.com. In order to do this I needed to create 2 virtual directory directives within my httpd.conf file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName blog.example.com
DocumentRoot /var/www/blog
</VirtualHost>
Immediately after this, I kept my rewrite code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
The problem is that this after adding the Virtual Directory code, the rewrite code is no longer working. I tried creating the following Directory directive, which doesn't seem to work at all.
<Directory "/var/www/html">
Options Indexes MultiViews FollowSymLinks
Order Deny,Allow
Allow from all
AllowOverride All
</Directory>
Additionally, I tried creating a .htaccess file in the html folder of the website and restarting apache, but nothing that I put in there is getting noticed at all.
Any help is greatly appreciated. Thanks!
Answer from comment by original poster:
Ok, I figured it out. I just needed to move the rewrite logic within the start and end Directives. Thanks for the help #animuson. – Henry Wrinkler