Detect HTTPS Proxy in PHP or .htaccess - php

I have got ProxyPass set up in the apache server config.
PHP seems cant detect the HTTPS.
How do i detect the HTTPS in PHP or redirect if not HTTPS in .htaccess?
Apache config:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName domain.co.uk
ServerAlias www.domain.co.uk
ProxyPass / http://external-domain.com/
Include /etc/letsencrypt/options-ssl-apache.conf
...
</VirtualHost>
</IfModule>

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Source: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

This will check in PHP if you're on HTTPS.
if(!empty($_SERVER["HTTPS"])) {
}

Related

Configuring Symfony 5 Application on Apache server

I need some help with configuring Symfony on apache2 web server.
My current project category structure is:
var/www/domain.com/public_html
Inside public_html is where my Symfony application is located.
I am stuck at figuring our what I am doing wrong in my either .htaccess or /etc/apache2/sites-available/domain.com.conf files.
I am using symfony/apache-pack. My .htaccess files live inside /public folder as per documentation. Here it is:
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
And here is my /etc/apache2/sites-available/domain.com.conf
<VirtualHost *:80>
ServerAdmin someemail#example.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com/public_html/public
<Directory /var/www/mydomain.com/public_html/public>
AllowOverride All
Order allow,deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I also read this and updated 000-default.conf as follows:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com/public_html/public
<Directory /var/www/domain.com/public_html/public>
Options FollowSymlinks MultiViews
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml >
</IfModule>
</VirtualHost>
For the moment I am still seeing symfony project folder structure.
I tried:
inside .htaccess changing DirectoryIndex to /public/index.php
inside .conf file changin DocumentRoot to /va/www/domain.com/public_html/public
tried clearing cache
SO far nothings seems to help.
Would be extremely helpful for any advice.
I see that you are editing vhost which begins with
<VirtualHost *:80>......
That mean your website should be reachable by 80 port or in other words by http protocol. But! If in reality you are accessing your website by https protocol then make sure you are editing right Apache2 .conf file.
If the website is reachable by https protocol (especially if you use LetEncrypt), then try locating /etc/apache2/sites-available/....-le-ssl.conf config file (or similar one with corresponding domain title and with ssl prefix or postfix in it's name).
After editing that file do not forget to restart Apache2 webserver with $sudo service apache2 restart and test website again.

API routes using Https on laravel gives 404

I created an Laravel API.
First, it was using HTTP, I needed to change it to use https.
So I created an account on Cloudflare and since then when I go to my API endpoints:
GET: https://www.traapp.tk/api/data/20190809 it gives a 404:
Not Found
The requested URL /api/data/20190809 was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
I also have a POST request and that returns a 404 to.
.htacces
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('data/{date}', 'MainController#index');
Route::post('route', 'MainController#getAllRoutesOfACertainDay');
MainController
public function index ($date) {
$responseServer = json_decode($this->makeRequest(str_replace('DATE', $date, env('BASE_URL') . env('SCHEDULES'))),true);
return $this->respond($responseServer);
}
I tried solutions like these:
Force Laravel to use HTTPS version
How to implement HTTPS in laravel 5.4
That's a 404 from your Web server not from your laravel. I guess you forgot to change the declared port in your Vhost configuration from 80 to 443.
Cloudflare example:
<VirtualHost *:443>
ServerName.....
First try to enter to GET routes, if you have 403 or 404, the problem is the nginx configuration, look the log, and if the problem is the doesn't find index.html in public_html is the root configuration of your nginx or apache2
Apache 2 has te config in the /etc/apache2/sites-enabled/ and your-domain.conf when you activate ssl in your site the problem can be, that the site generates another conf file, like your-domain.ssl.conf and this file looks like
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin info#domain.com
DocumentRoot /home/admin/web/domain.com/public_html/public <----- here is the change
ScriptAlias /cgi-bin/ /home/admin/web/pdomain.com/cgi-bin/
Alias /vstats/ /home/admin/web/domain.com/stats/
Alias /error/ /home/admin/web/domain.com/document_errors/
#SuexecUserGroup admin admin
CustomLog /var/log/apache2/domains/domain.com.bytes bytes
CustomLog /var/log/apache2/domains/domain.com.log combined
ErrorLog /var/log/apache2/domains/domain.com.error.log
<Directory /home/admin/web/domain.com/public_html>
AllowOverride All
SSLRequireSSL
Options +Includes -Indexes +ExecCGI
php_admin_value open_basedir /home/admin/web/domain.com/public_html:/home/admin/tmp
php_admin_value upload_tmp_dir /home/admin/tmp
php_admin_value session.save_path /home/admin/tmp
</Directory>
<Directory /home/admin/web/domain.com/stats>
AllowOverride All
</Directory>
SSLEngine on
SSLVerifyClient none
SSLCertificateFile /home/admin/conf/web/ssl.domain.com.crt
SSLCertificateKeyFile /home/admin/conf/web/ssl.domain.com.key
SSLCertificateChainFile /home/admin/conf/web/ssl.domain.com.ca
<IfModule mod_ruid2.c>
RMode config
RUidGid admin admin
RGroups www-data
</IfModule>
<IfModule itk.c>
AssignUserID admin admin
</IfModule>
IncludeOptional /home/admin/conf/web/sapache2.domain.com.conf*

Can't seem to get redirect from https to http working in conf file

Please take a look at my .conf file. I am trying to redirect people visiting my site on https to http as I need to sort some other stuff before I can renew the SSL cert so for the time being need to redirect all visitors to http://
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/myproject
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
// method 1 - Not working
RedirectMatch permanent ^(.*)$ http://www.example.com$1
// method 2 - Not working
Redirect / http://www.example.com/
// method 3 - Not working
RewriteEngine On
RewriteCond %{HTTPS} On
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
// method 4 - Not working
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1
DocumentRoot /var/www/myproject
</VirtualHost>

Apache mod_rewrite remove a folder in url and php extension

Hey,
I am trying to make my url a bit prettier with apache mod_rewrite.
html (root)
css
js
layout
sites (folder)
profil.php
work.php
index.php
At the moment my url looks like this:
https://example.com/sites/profil.php
https://example.com/sites/work.php
And I want that it looks like this:
https://example.com/profil
https://example.com/work
How can I can I overwrite the "sites folder" so, there is none and is it possible to remove the .php extension?
Thats how my apache config looks like:
<VirtualHost example.com>
DocumentRoot /var/www/example.com/html
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example.com/html">
allow from all
Options None
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Do I need to edit my links after that change in my files?
Here are your mod rewrite rules
# Rewrite --- https://example.com/sites/profil.php => https://example.com/profil
RewriteRule ^profil$ sites/profil\.php [NC,L]
# Rewrite --- https://example.com/sites/work.php => https://example.com/work
RewriteRule ^work$ sites/work\.php [NC,L]
enter this into your .htaccess in the right location and give it a try.
<VirtualHost example.com>
DocumentRoot /var/www/example.com/html
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example.com/html">
allow from all
Options None
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteRule ^sites/profil\.php$ /profil?&%{QUERY_STRING}
RewriteRule ^sites/work\.php$ /work?&%{QUERY_STRING}
</VirtualHost>

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>

Categories