htaccess issue on codeigniter setup on ubuntu - apache

My website open properly when I type `www.abc.com` on browser but when I type `abc.com` it opens page of subdomain which I have hosted which is `admin.abc.com`.
But the url still says `abc.com`
Here is my .htaccess code:
<!-- language: lang-bash -->
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I am using Ubuntu 14.04 with Apache and have configured virtual hosts for both domains.
Any guidance will be highly appreciated.
Here is my conf file for abc.com which is uploaded on the server for both domain and sub-domain.
<!-- language: lang-bash -->
# domain: abc.com
# public: /var/www/abc.com/public_html/
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/ssl/localcerts/c8d39f3574580de6.crt
SSLCertificateKeyFile /etc/ssl/localcerts/abc.key
SSLCertificateChainFile /etc/ssl/localcerts/gd_bundle-g2-g1.crt
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin admin#abc.com
ServerName abc.com
ServerAlias abc.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/abc.com/public_html
# Log file locations
LogLevel warn
ErrorLog /var/www/abc.com/log/error.log
CustomLog /var/www/abc.com/log/access.log Combined
</VirtualHost>
Here is my conf file for admin.abc.com
<!-- language: lang-bash -->
# domain: admin.abc.com
# public: /var/www/admin.abc.com/public_html/
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin admin#abc.com
ServerName admin.abc.com
ServerAlias admin.abc.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/admin.abc.com/public_html
# Log file locations
LogLevel warn
ErrorLog /var/www/admin.abc.com/log/error.log
CustomLog /var/www/admin.abc.com/log/access.log combined
</VirtualHost>
do let us know if above conf files where helpful to determine the issue, Looking forward to your response.

Do you have your Virtual Host configuration correctly configured?
I would recommend you using a configuration file instead of .htaccess if you are able to.
(under: /etc/apache2/sites-enabled or sites-available, if it has not been activated yet)
<VirtualHost *:80>
# With this configuration when you try to reach both www.abc.com
# and abc.com it directs you to the same place.
# If you got a subdomain, you should create a separate .conf file
# with the correct configurations as well.
ServerAdmin admins#email.com
ServerName www.abc.com
ServerAlias abc.com
DocumentRoot /var/www/path/to/websites/folder
# Some more comments...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# More comments...
</VirtualHost>
After editing this, do not forget to restart the Apache server:
sudo service apache2 restart
or
sudo systemctl restart apache2.service
Hope it helps.

Related

Alias inside VirtualHost is not working and gives 404 error

I have a server which maintains multiple domains using VirtualHost and I'm trying to setup GitWeb according to Gitweb - ArchWiki.
According to the wiki, I have to append the lines below to an Apache config file.
Alias /gitweb "/usr/share/gitweb"
<Directory "/usr/share/gitweb">
DirectoryIndex gitweb.cgi
Options ExecCGI
Require all granted
<Files gitweb.cgi>
SetHandler cgi-script
</Files>
SetEnv GITWEB_CONFIG /etc/gitweb.conf
</Directory>
After this, Alias works and I can access the service by accessing one of
192.168.10.102/gitweb/ (local IP)
https://my_domain_1.com/gitweb/
https://my_domain_2.com/gitweb/
...
So far, everything's okay.
Now I'd like to limit the effect of the Alias to one domain: my_domain_1.com. So I moved the lines above inside its existing VirtualHost settings.
<VirtualHost *:80>
DocumentRoot "/var/www/html1"
ServerName my_domain_1.com
# The lines are pasted here.
RewriteEngine on
RewriteCond %{SERVER_NAME} =my_domain_1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} END,NE,R=permanent]
</VirtualHost>
However, when I access https://my_domain_1.com/gitweb/, 404 error occurs. The error log says nothing and I have no .htaccess for the domain.
How can I make it work?
I found the solution.
Since I requested that all of http requests should be redirected to the https versions, I should have written the gitweb settings not in <VirtualHost *:80> (http) but in <VirtualHost *:443> (https). The result should be
<VirtualHost *:443>
DocumentRoot "/var/www/html1"
ServerName my_domain_1.com
# The lines are pasted here.
SSLCertificateFile /etc/letsencrypt/live/my_domain_1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/my_domain_1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
Now it works perfectly.

htaccess for url redirection with virtualhosts

I have an Apache server running 3 virtual hosts:
<VirtualHost *:80>
DocumentRoot "/var/www/sub1"
ServerName sub1.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub2"
ServerName sub2.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub3"
ServerName sub3.domain.com
# Other directives here
</VirtualHost>
I would like to set up URL redirection on sub3 so that when sub3.domain.com/go/7dj29 is requested, it redirects to sub3.domain.com/go/redirect.php?id=7dj29.
I've created an .htaccess in /var/www/sub3:
RewriteEngine On
RewriteRule ^\/go\/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1 [QSA,L]
However, I receive a 404 instead of a redirect; what am I doing wrong?
Not Found
The requested URL /go/7uja8 was not found on this server.
The following rule works.
RewriteRule ^go/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1
For apache to load rules from .htaccess you need at least AllowOverride FileInfo, but you can set it directly in VirtualHost context without it.

Apache virtual host directory domain mapping clash

By clash I mean that one domain works, it leads to /var/www/html and reads the main index.php file however the second domain which leads to /var/www/html/website gives me a permission denied for root directory "/" so I'm wondering if it is interpreting /var/www/html as /
Or is it something else?
Thank you for any help
Virtual host setup port 80 only
# works
<VirtualHost *:80>
ServerName www.site.us
ServerAlias site.us http://www.site.us
DocumentRoot /var/www/html
# CustomLog /directory log file location not enabled
# ErrorLog /directory log not enabled
</VirtualHost>
# both site2.com and www.site2.com have 404 problem
# The requested URL / was not found on this server.
<VirtualHost *:80>
ServerName www.site2.com
ServerAlias site2.com http://www.site2.com
DocumentRoot /var/www/html/site2
# CustomLog /directory log file location not enabled
# ErrorLog /directory log not enabled
</VirtualHost>
# works fine
<VirtualHost *:80>
ServerName www.site3.com
ServerAlias site3.com http://www.site3.com
DocumentRoot /var/www/html/site3
# CustomLog /directory log file location not enabled
# ErrorLog /directory log not enabled
</VirtualHost>

How to serve static files using apache?

I have configured apache 2.2 webserver with tomcat. I want static files of my web application to be served using apache. I made a virtual host entry in httpd.conf
<VirtualHost *:8080>
Alias webcommon_alias C:/webcommon_bk
JkMountFile D:/xampp/apache/conf/myexample.net.properties
ServerName myexample.net
ErrorLog logs/myexample.net-error_log
CustomLog logs/myexample.net-access_log common
<Directory C:/webcommon_bk>
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /webcommon/
RewriteRule ((.*)\.(js|css|html|gif|png)$) webcommon_alias/$1
</Directory>
</VirtualHost>
But when i fetch this file in browser http://myexample.net:8080/webcommon/img/aboutBox.png i get Object not found! The requested URL was not found on this server.
Try this instead:
<VirtualHost *:8080>
Alias /webcommon C:/webcommon_bk
JkMountFile D:/xampp/apache/conf/myexample.net.properties
ServerName myexample.net
ErrorLog logs/myexample.net-error_log
CustomLog logs/myexample.net-access_log common
<Directory C:/webcommon_bk>
Order allow,deny
Allow from all
RewriteEngine On
RewriteRule ((.*)\.(js|css|html|gif|png)$) /webcommon/$1.$2
</Directory>
</VirtualHost>

accessing site over https causes folder structure to be shown in browser, not application

I have a rails 3 app running with following stack:
ubuntu lucid lynx, apache2, passenger hosted on Amazon EC2
When accessed over http, the pages load and the site has no problems at all.
Then i installed a valid SSL cert on the webserver. When i try to access the same url over https, and it looks like passenger is not being activated. instead, in the browser, the folder structure of the rails application is rendered.
What configuration changes or modifications do I need to make to Apache and/or Passenger so that the https URL returns the same thing as the corresponding http url?
You need something like this:
<VirtualHost *:80>
ServerName orders.xxx.com
DocumentRoot "/var/www/html/ps-main/current/public"
CustomLog /var/log/apache2/orders common
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://orders.xxx.com%{REQUEST_URI}
NameVirtualHost *:443
RailsEnv production
<Directory /var/www/html/ps-main>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
CustomLog /var/log/apache2/orders common
ServerAdmin webmaster#xxx.com.com
ServerName orders.xxx.com
SSLCertificateFile /etc/ssl/cert.crt
SSLCertificateKeyFile /etc/ssl/server.key
SSLCertificateChainFile /etc/ssl/intermediate.pem
# SSLProtocol all
SSLCipherSuite HIGH:MEDIUM
DocumentRoot /var/www/html/ps-main/current/public
<Directory /var/www/html/ps-main/current/public>
Options -MultiViews
AllowOverride all
</Directory>
</VirtualHost>
This will redirect all http traffic to https. If that's not what you need, remove the redirects.