Keep all the site under SSL excepet two subfolders, without .htaccess - php

I would like to know if is possible to keep all the site under ssl except two sub folders that works as a endpoint for external calls with apache.
I've my site (www.example.com) that user can see only trought https and I would like to keep example.com/subfolder1 and example.com/subfolder2 reachable also trough the normal http protocol. Is it possibile?
This is the .conf file of apache.
<VirtualHost 127.0.0.1:8080>
ServerName example.com
ServerAlias www.example.com
Redirect / https://www.example.com/
</VirtualHost>
I'm trying to avoid the use of the .htaccess file, is it possibile? Thanks to all!

You could use a Rewrite (instead of a Redirect) so you can set a condition. Maybe like this:
<VirtualHost 127.0.0.1:8080>
ServerName example.com
ServerAlias www.example.com
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(subfolder1|subfolder2)/? [NC]
RewriteRule ^(.*) https://www.example.com%{REQUEST_URI} [R,L]
</VirtualHost>
You have to enable the rewrite module for that too:
LoadModule rewrite_module modules/mod_rewrite.so

Related

Apache virtual host always redirecting to /dashboard

I'm having what appears to be a common problem but any solutions I've found don't seem to work for my case.
I'm trying to set up a virtual host so that I can access the public file of my Laravel installation by going to "mytestdomain.local" but when I type this address into google chrome I am always redirected to the xampp dashboard at this address "https://mytestdomain.local/dashboard/".
I've installed Laravel in the following xampp directory: c:/xampp/htdocs/mytestdomain_uk.
I have "C:\xampp\apache\conf\extra\httpd-vhosts.conf" set up as follows:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mytestdomain_uk/public"
ServerName mytestdomain.local
</VirtualHost>
And I have "C:\Windows\System32\drivers\etc\hosts" set up as follows:
127.0.0.1 localhost
127.0.0.1 mytestdomain.local
If anyone could offer any insight into this issue I would be very grateful.
Put this as the first line in C:\...\httpd-vhosts.conf (and restart the web server):
NameVirtualHost *:80
So, it should look like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName walkpeakdistrict.local
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public"
</VirtualHost>
I would place all my projects somewhere outside of C:/xampp/htdocs and C:/xampp. Let C:/xampp/htdocs be the standard localhost location, with just two files inside (simple index.php and index.html), but use another one for the projects. Even better, use another partition, not the system partition C:. Like D:/projects, or so. So, you would have D:/projects/walkpeakdistrict_uk.
Good luck.
Ok, I'm not sure why this was an issue but it seems to work when I change the virtual host's server name to anything other than ".local".
Thanks again to all who replied!
This happened with me as well. And I resolved it. In my case, it was happening because of the SSL module. So, turning it off got me out of the trouble. Just Open the httpd.conf file and comment the SSL include code by adding # to the front of the code as given below.
# Secure (SSL/TLS) connections
# Include conf/extra/httpd-ssl.conf
After this, it will be resolved.
I'm pretty sure the issue for me had to do with SSL redirects, for some reason. When I edited my .htaccess to exclude local.mydomain.com when forcing https, I stopped getting redirected to the XAMPP dashboard.
Below is the section of my .htaccess that excludes local sites from redirecting to https. You can add extra RewriteCond lines for other local domains.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !local\.
RewriteCond %{HTTP_HOST} !other.localdomain.example.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
</IfModule>
<IfModule mod_headers.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !local\.
RewriteCond %{HTTP_HOST} !other.localdomain.example.com
Header set Strict-Transport-Security "max-age=16070400" env=HTTPS
</IfModule>
Change your document root to this, just add a slash at the end of public and it will work
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public/"
I had this problem after developing my website and I got round it by simply rebooting my machine. Many months later, Google then invalidated the use of .dev so my virtual host stopped working. I changed the name of my virtual host to .test and got the dashboard. I had forgotten the simple solution and arrived on this page searching for a solution. Then i remembered......I rebooted my machine and hey presto....it worked. No more dashboard. I get the right homepage.
In your case, you put localhost settings in virtual host. check if you got this script on default file conf, then comment it. Case on xampp for mac
Include "${your_xampp_path}/conf/httpd.conf"
The solution is very simple, it always redirects the https url to the Dasboard, you just have to remove the s leaving http:
Example:
http://juego123.com
I was facing the same error. I solved the error by removing the *80 port number from the virtual host tag and then my code look like
<VirtualHost *>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *>
ServerName walkpeakdistrict.local
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public"
</VirtualHost>
**NOTE:**Your Virtual Host tag looks like this <VirtualHost *>
This means we are redirecting to any other port or not bounding the virtual host to port 80 this will work definitely.
I came here with the same problem, in my case accessing the website through http instead https worked.
In browser, instead of accessing this way:
https://mytestdomain.loc
try this one
http://mytestdomain.loc
Its works for me once I remove header location from 'htdocs/index.php' file

How to perfectly set up virtual host for codeigniter project?

I am trying to make a virtual host for a codeigniter project. I have done this in httpd-vhosts.conf:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login">
Require all granted
</Directory>
</VirtualHost>
and in application/config/config.php,
$config['base_url'] = 'http://dev.facebook-login.com';
and
$config['index_page'] = '';
the browser opens the landing page. but when transiting from any other uri it says object not found.
And when i configure httpd-vhosts.conf like this:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login\index.php"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login\index.php">
Require all granted
</Directory>
</VirtualHost>
It arises problem with assets things, i,e images and some css doesnt loads. How can I solve it?
I am on windows 10 and I use xampp with virtual host this is way I set up.
Put forward slash at end of base url
$config['base_url'] = 'http://dev.facebook-login.com/';
First go to
Windows > System32 > drivers > etc > host
You may need to open as administrator to be able to save it
You might see some thing like
127.0.0.1 localhost
Below that create another one for dev.facebook-login.com like example:
127.0.0.1 localhost
127.0.0.1 dev.facebook-login.com
Save it
Then go to the
xampp > apache > conf > extra > httpd-vhosts.conf
open it as administrator so you can save it.
No need for the index.php on DocumentRoot
<VirtualHost *:80>
##ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/xampp/htdocs/CI_projects/facebook-login"
ServerName dev.facebook-login.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
Save it and restart the severs.
I use this for my htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
httpd-vhosts.conf
Listen 1122
<VirtualHost *:1122>
DocumentRoot "D:\laragon_server\www\hugpong_production"
<Directory "D:\laragon_server\www\hugpong_production">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
https://www.namecheap.com/support/knowledgebase/article.aspx/10026/33/installing-an-ssl-certificate-on-xampp/
php 8 is not ready for CI4 as of jan 2021!
XAMPP has preloaded ssl certificate in php 7 packages
consider uncommon vhosts extra and place this in it
my only persistent issues is codeigniter 4 routes, have to turn on php spark serve to get XAMPP to route pages outside of default index, nothing else works, its garbage to that extent, will try with composer next....a whole book needs
to be wrote about codeigniter 4 routes and why they don't work, 200 pages plus
<VirtualHost localhost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
<VirtualHost example.com *:8080>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
//my project folder is codeignite4 without using composer this time
//i added listen 8080 under listen 80 in htppd.conf
ServerName example.com
<VirtualHost truservex.com *:443>
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
ServerName example.com
SSLEngine On
SSLCertificateFile "C:/xampp/apache/conf/ssl.crt/server.crt"
// you can verifiy these file locations, certificate data
SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.key/server.key"
// going without the Directory tag can be helpful to drop in vhosts
<Directory "C:\xampp\htdocs\codeignite4\public">
Require all granted
doing this i was able to replicate the original install, create site access
using http://localhost:8080 or http://example.com/ as my base url
in my example index.php is removed, set to ''
when using command prompt to get into codeignite4 project folder i can
launch php spark serve and routes will work that were previously 404
Using Xampp 7.3.25 Dec 2020 because php 8 will not run error
free with codeigniter 4 as of this date, this worked for me as
my first codeigniter 4 virtual host setup, my install had
difficulty initially with using port 80, seems to want to for
port 443 if it can..I'm not a server pro but i wanted to
leave this for the next guy....This is strictly name based
virtual hosts for develop, ip based would have obvious
advantages. Because i had trouble in initial CI4 testing with
port 80 the (*) in virtual hosts vs (*:80) seemed to solve one
hurdle in searching for more ports, now i can finally begin my
first project ...my default URL was also changed to example.com
in appstarter/apps/config file. Carefully reread <tags> for
typos, its easy to shut server down!
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/"
ServerName localhost
<Directory "C:/Xampp/htdocs/">
Require all granted
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/appstarter/public/"
ServerName example.com
ServerAlias www.example.com
# maybe don't attempt to use dir tag, shuts down server
# the directives at httpd.conf appear to suffice
I have added a htaccess file in the appstarter/public
I assume this is primitive, can get far more advanced
The Apache help pages helped me focus a bit more on
what i must accomplish, name based virtual hosts this case
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
2.2 Apache configuration: :Apache server context important!
Order allow,deny :The hackers are winning.....
Allow from all
2.4 Apache configuration:
Require all granted

HTTPS redirect to specific URL

I have the below redirect set in httpd.conf which redirects subdomain.domain.com to domain.com/topics/apps
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [OR]<br>
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com.gov$<br>
RewriteRule ^/(.*)$ http://www.domain.com/topics/apps[R=301]
But if i try the same with https, it wont redirect to that page but shows domain.com homepage (not redirect). I guess this is because the DNS entry for subdomain.domain.com is also having the same IP as domain.com.
Can you guys suggest fixing this https redirect to http://domain.com/topics/apps.
Thanks,
Sudeer.
You need to set up a Virtual Host on Port 443 and also set it to Named Virtual Hosts when the IP is the same but the names are different.
NameVirtualHost *:443
<VirtualHost *:443>
ServerName myserver.de
RewriteEngine On
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 0
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com.gov$
RewriteRule ^/(.*)$ http://www.domain.com/topics/apps[R=301]
</VirtualHost>
My experience is that Apache, by default, handles SSL with a separate VirtualHost on a different port. Look for the VirtualHost which is listening on 443 and set the redirection there.
Do you have access to the Vhosts ? If so, I would skip the rewrites and simply do this(and restart httpd):
<VirtualHost *:443>
ServerName subdomain.domain.com
ServerAdmin you#you.com
DocumentRoot this/needs/to/exist
Redirect permanent / http://domain.com/topics/apps
</VirtualHost>

Silent redirect based on domain

I've set up my own server.
Let's say my main server folder is /home/www and I've set the nameservers so that all domains point to this location.
Now, here's what I want to do :
Have each domain's files in a separate subfolder
Based on the domain requested, silently redirect to the appropriate subfolder
E.g.
if we need somedomain.com or www.somedomain.com or www.somedomain.com/anything/ (or any such variation for that matter) redirect the request from /home/www/ to /home/www/somedomain.com/
How can this be done?
And here's what I've tried (but given that .htaccess is definitely ... not my thing, it'll most likely be close to non-sensical...) :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?somedomain.com$ [NC]
RewriteRule ^(/)?$ somedomain.com [L]
There are two possible choices. you can create Virtual Host for each domain and set virtual document root.
Or
You can use rewrite rule.
From apache Virtual host documentation:
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/www.example1.com
ServerName www.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/www.example2.org
ServerName www.example2.org
# Other directives here
</VirtualHost>
You can find out more in http://httpd.apache.org/docs/2.2/vhosts/examples.html

Redirect with www not working

I recently switched from a shared hosting to a VPS.
Everything is working fine and I can access al of my domains.
But I seem to have 1 problem:
In the past all non-www url's where redirected to url's with www.
I did this with the following htaccess.txt code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.tx3\.be$ [NC]
RewriteRule ^(.*)$ http://www.tx3.be/$1 [R=301,L]
But I'm using a new htaccess at the time with the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^off(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But both are not working on redirecting my non-www domain.
When I visit my non-www domain, I get the following page:
"Apache is functioning normally"
I tried using the DirectAdmin redirect for redirecting the non-www domain to a domain with www, but that also didn't work.
Maybe I've got it all wrong and it wasn't a htaccess problem in the first place.
Did I forget something in my server settings?
I don't know, everything looks to be in order.
(I did double check my DNS settings and they appear to be fine)
Does anyone know what I should be doing next?
Please try this
Rewriteengine on
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If using windows download putty (ssh client) give it your servers ip address select type as ssh and click open.
It'll ask you for a username and password give it your root username and password (should have been provided by vps hosting company)
Now cd through to your apache config folder (I'm only familiar with debian based setups if you're using a redhat based setup you may have to google correct path) /etc/apache2/sites-available
Now look at the directory listings (ls) and it'll show you the apache configs available chances are you'll have something like www.tx3.be in there. Edit that file I recommend using pico or nano by doing pico
Here's an example off my server
root#hinch:/home/hinch# cd /etc/apache2/sites-available
root#hinch:/etc/apache2/sites-available# ls
default
default-ssl
root#hinch:/etc/apache2/sites-available# pico default
Now you're in your apache config file you'll see something like this
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#yourdomain.com
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm default.htm default.html default.php
Options Indexes FollowSymLinks MultiViews
AllowOverride None
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
</VirtualHost>
After the line that says ServerAdmin add in the following
ServerAlias tx3.be
ServerAlias www.tx3.be
Save the file and restart apache
service apache2 restart or service httpd restart
Delete all redirect code from your .htaccess and make sure you have both the www. dns A record and the #. A record pointing to your servers ip address. Now regardless of which version of the domain the user goes to they'll see your site.

Categories