I would like to drive my environment (dev, prod) by URL
myuser.ENV.domain (eg : foo.dev.test.com or foo.prod.test.com)
So I decide to do some changes in .htaccess file
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_NAME} mydevserver.priv?$
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(mydevserver.priv)?(:80)?$
RewriteRule .* - [E=SF_USER:%1,E=SF_ENV:%2,E=SF_SERVER:%3,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
Next, I do some changes in app.php
<?php
ini_set('memory_limit', '256M');
umask(0000);
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
$env = filter_input(INPUT_SERVER, 'SF_ENV') ?: $_SERVER['SF_ENV'];
/** #var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../app/autoload.php';
if ('dev' === $env) {
$debug = true;
Debug::enable();
} elseif ('prod' === $env) {
include_once __DIR__ . '/../var/bootstrap.php.cache';
$debug = false;
} else {
throw new \Exception('Environment Not handled');
}
$kernel = new AppKernel($env, $debug);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
When I go on the prod URL, I have the default success page of Symfony 3, without debug toolbar.
When I go on the dev URL, I have the default success page but I don't have the debug toolbar. There is a 404 error about _wdt & _profiler routes.
But, If I go on the file app_dev.php, I have the debug tool bar.
I probably miss a little thing but I can't figure out.
I did a fast test renaming app.php into index.php and remove the DirectoryIndex but it changes nothing.
If I user var_dump on my $env var, it gets the good env from environment variable SF_ENV.
Can someone help me ?
Not sure what's wrong exactly but setting up a virtual host might be simpler solution providing same results.
You have all the settings needed in Symfony virtual host documentation. If you choose to go that way and you are using Apache 2.4+ follow the mod_php settings. Also this version of Apache server has replaced
Order Allow,Deny
Allow from All
with:
Require all granted
You also have youtube video tutorials on how to setup virtual hosts. Here's an example of one of my virtual hosts which I made following the above mentioned documentation, vhost for a simple symfony app for testing:
<VirtualHost *:80>
ServerName www.login.com
DocumentRoot "C:/xampp/htdocs/symfUser/web"
<Directory "C:/xampp/htdocs/symfUser/web">
Require all granted
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory "C:/xampp/htdocs/symfUser/web/bundles">
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
Related
I have installed Codeigniter 4 on Raspbian and everything seems to be working fine.
My web directory is /var/www/html
Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:
blue.ddns.net -> /var/www/html/blue/public/index.php
black.ddns.net -> /var/www/html/black/public/index.php
So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests
Well, I'm still at step 0 because I can't get the .htaccess file to work properly.
Below I attach a copy of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The Rewrite module is activated
the error that appears in /var/log/apache2/error.log is:
/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.
Go into your sites-avaiable folder.
$ cd /etc/apache2/sites-available
Create a new virtual host called blue.ddns.net.conf
$ touch blue.ddns.net.conf
Open that file with nano or any other text editor you might like and add the following.
<VirtualHost *:80>
ServerAdmin your#email.com
DocumentRoot /var/www/html/blue/public
ServerName blue.ddns.net
ServerAlias blue.ddns.net
<Directory "/var/www/html/blue/public">
allow from all
AllowOverride All
Options None
Require all granted
</Directory>
RewriteEngine on
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =blue.ddns.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Save and close the file.
Then add this to your apache with the following:
$ sudo micro blue.ddns.net.conf
Repeat the same process for the other codeigniter install and reboot apache.
$ sudo service apache2 restart
That should do it.
Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.
I've always worked in hosting and never had to do Apache configurations.
But now I'm discovering the Raspberry/Linux world.
Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:
Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:
<Directory "/var/www/html">.
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
in /var/www/html instead I created a .htaccess file with the following code:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
RewriteRule ^(.*)$ black/public/index.php?/$1 [L]
RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
apps are located in
/var/www/html/blue
and
/var/www/html/black
It's a very simple solution that serves my purposes.
Although I think it's not very elegant
What do you think about it?
I have a symfony app in my /var/www/html/app/ and I'd like to access it via host/app/ but I can't make it work.
I have a conf file with this
<Directory /var/www/html/app>
AllowOverride All
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
</Directory>
It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.
And this htaccess in /var/www/html/app/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
With this .htaccess, When I go to ://host/app I go to the symfony app (not the apache directory structure) and that seems to be a good start.
But the problem is I'm getting an Exception No Routes found for /app/.
That's normal because /app/ is the subfolder in my server, not the start of symfony routes.
How can I make the symfony routes beginning after the /app/ folder path ?
I know there is some questions about symfony2/3 routing in subfolder but the answer requires to separate src and public files, that's not what I'd like to have.
I just want my routes working when accessing ://host/app.
For information, It works great when I access ://host/app/public/*Symfony routes*
Edit:
New app.conf
Alias /rapp "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!public/index.php)$ public/index.php$1 [L]
</IfModule>
</Directory>
I managed to resolve the problem !!!
I'm not sure the config is "proper" though but it seems to work.
Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)
Alias /app "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule (/public) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
</Directory>
The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1.
As page-1 is not a directory or a file it will loop infinitely.
So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.
I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.
If someone have ideas to improve this config, feel free to leave comment.
The problem is the that your web server's document root (the directory that apache will make public via the url) and your project's document root (the public directory) are not the same. Instead your server points to the project directory. That is why you have to manually go into the project's document root by adding the public/ to the url.
You should avoid making the project directory accessible as this could expose your configuration and thus make your website vulnerable, e.g. by giving people access to your database through the credentials inside the config.
You might be able to achieve your goal by storing the project somewhere else and then only symlink your project's public dir to /var/www/html/app. This will probably require some changes to your apache config, e.g. adding Options FollowSymLinks.
Another, more elaborate, approach could be using mod_proxy. The configuration will roughly look like this:
# Your public server reachable from the outside
<VirtualHost *:80>
ProxyPass /app http://127.0.0.1:8080/
</VirtualHost>
# Your internal server where the requests are proxied to
<VirtualHost 127.0.0.1:8080>
<Directory /var/www/html/app/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
...
</VirtualHost>
As you can see the internal web server points to your project's document root (public dir). You might have to do additional both in the apache setup and app, e.g. make sure trusted proxies are set correctly or adding ProxyPreserveHost On to the apache setup, but roughly this should point you in the right direction.
edit: I missed another obvious solution, using mod_alias to point your symfony app to /app:
Alias /app /var/www/html/app/public
I've tried almost every possible option with .htaccess file in my lumen's public folder but non of them are giving me Pretty URLs.
In previous versions of Lumen I hadn't touched any of the .htaccess rules and yet everything used to work perfectly with the shipped default .htaccess file.
But in this new version 5.2 nothing seems to work.
the default is as follows :
# Apache configuration file
# http://httpd.apache.org/docs/2.2/mod/quickreference.html
# Note: ".htaccess" files are an overhead for each request. This logic should
# be placed in your Apache config whenever possible.
# http://httpd.apache.org/docs/2.2/howto/htaccess.html
# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Any thoughts ?!
In you apache config file (httpd.conf or apache2.conf) you must change the "AllowOverride" to "All":
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
I've set my document root as follows:
c:/wamp/www/laravel/public
When I test localhost on the browser I get the "You have arrived" page.
However when I try localhost/page0, I get a 404.
In my routes.php file I have this:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('page0', function() {
return 'Test Helloworld! 0';
});
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
It looks like everything should work. Why doesn't this work?
You might want to check if mod_rewrite is enabled by doing:
phpinfo();
You can issue this in your routes.php.
It is also possible to start a webserver in php like so from the terminal or command prompt depending on your OS:
php -S localhost:8080 -t public/
Do this from your project root. Then you can go to:
localhost:8080
And your site should be up and running. You can still use the MySQL server that is running from XAMPP or WAMPP.
Use
c:/wamp/www/laravel/public/page0
This should work.
For production and/or development you can set the document root to the "public" folder, to get rid of the "/public/" in your url:
#see https://stackoverflow.com/a/15469480/1854296 (for example)
I'm having an issues with mod rewrite displaying the link in the browser correctly
I have always put links on my pages like this
http://domain.com/contact-us.html
and then use this in my .htaccess file
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([^.]+)\.html$ http://domain.com/index.php?h=$1 [L]
I request the h variable on the index.php page to get and use the slug "contact-us"
$h = htmlspecialchars($_REQUEST['h'], ENT_QUOTES, 'UTF-8', false);
$h = preg_replace('/[^-a-z0-9_]/i','',$h);
This has always worked fine and will still show the domain.com/contact-us.html in the browser
I loaded a site on 1 and 1 and instead of seeing domain.com/contact-us.html I see domain.com?h=contact-us in the browser. Everything is working except it is not showing the link correctly
Got it but for those it may help in the future. First clear your cache with each test
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([^.]+)\.html$ /index.php?h=$1 [L]
Don't include the domain. In my case the website was up one level in the public_html folder so I thought I had to include the domain.
added
RewriteBase /
Works great, final code
Options FollowSymLinks
RewriteEngine On
RewriteBase /
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([^.]+)\.html$ /index.php?h=$1 [L]