Laravel assets, routes and pages - php

I think this is an easy one, but i'm really confused myself.
myproject.com/index.php shows my project main page
myproject.com shows my project main page too.
In 1st case, after routing it works fine (i.e. : myproject.com/index.php/register)
In 2nd case, after routing it fails (i.e. : myproject.com/register) with following error : The requested URL /register was not found on this server.
So because of that, i thought that i had to re-route every request for myproject.com to myproject.com/index.php
app/config/app.php :
'url' => 'http://127.0.0.1/public',
etc/httpd/conf/httpd.conf :
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/"
ServerName myproject.com
</VirtualHost>
Nothing's been configured in hosts file.
Goal: how can i re-route all requests from myproject.com to myproject.com/index.php with hiding index.php part of from users ?
Visitors should see that : myproject.com
but i want them to actually reach : myproject.com/index.php
Workaround:
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/index.php"
ServerName myproject.com
</VirtualHost>
Result : I cannot reach the assets, (js, css files) results with 404 because my browser is not allowed to reach myproject.com/public/. Root has been set as myproject.com/public/index.php in this virtualhost settings.

Check if the .htaccess file is present in your public directory.
If not check the Pretty Url Section.
Also make sure that you put <Directory> directives in your vhost configuration just to be on the safe side.
Eg:
<Directory "/var/www/html/myproject/public/">
AllowOverride All
Allow from All
</Directory>
Concerning the 'url' => 'http://127.0.0.1/public', in app.php am pretty much sure the /public is kinda extra.
'url' => 'http://127.0.0.1', should be working fine.

You need rewrite rules for this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Laravel docs: http://laravel.com/docs/5.1#pretty-urls

Related

php links not working in sub folder of multi site

Testing two identical folders , one placed in a Drupal install folder and the other in a plain html subfolder in
home root , only the php links for the one in CMS install work. This makes sense because Drupal has a settings php
file and .htaccess file which makes sure everything is in its proper place for links to work whether in home root or not. However I thought it would be easy enough to get the links to work in plain html subfolder with a simple rewrite
rule in the .htaccess file which exists in the php folder. Yet try as I might, nothing has worked so far.
The configuration in /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
ServerName drupalsite.com
ServerAlias www.drupalsite.com
ServerAdmin vps#drupalsite.com
DocumentRoot "/var/www/html/drupalsite.com"
<Directory /var/www/html/drupalsite.com>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName plainhtml.com
ServerAlias www.plainhtml.com
ServerAdmin vps#plainhtml.com
VirtualDocumentRoot "/var/www/html/plain”
</VirtualHost>
The .htaccess file in identical php folders
RewriteEngine On
RewriteBase /phpfolder/
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
So drupalsite.com/phpfolder links all work
But plainhtml.com/phpfolder links do not work
phpfolder for Drupal resides in /var/www/html and Drupal site is symlinked
phpfolder for plain html subfolder to root resides in /var/www/html/plain
Both phpfolders are owned by apache:root and all files within are owned by root:root
Have also tried changing the .htaccess rewrite rule to:
RewriteBase /
RewriteBase /plain/
RewriteBase /plain/phpfolder/
You will have to define a Directory entry with AllowOverride for:
/var/www/html/plain
<Directory /var/www/html/plain>
AllowOverrideAll
Allow from all
</Directory>
Note: If you have access to the main configuration file of the server you shouldn't be using .htaccess, configuring Rewrites or Redirects in Virtualhost is simpler. As you have already seen, using sub-files and per-dir configurations make things more complicated, not counting the overhead of apache having to constantly check those files when AllowOverride is enabled.

Modify Symfony2 routing to prepend all requests with application name

I want to host two instances of the same Symfony2 application on one Apache server. Each application should be addressed like so:
http://myserver/app1/
http://myserver/app2/
To do so I have set up two port based virtual hosts in Apache plus the third one on port 80 that redirects folder requests to proper vhosts:
<VirtualHost *:80>
DocumentRoot /var/www
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/app1/(.*)$
RewriteRule ^ http://%{HTTP_HOST}:9999/%1 [L]
RewriteCond %{REQUEST_URI} ^/app2/(.*)$
RewriteRule ^ http://%{HTTP_HOST}:8888/%1 [L]
</VirtualHost>
<VirtualHost *:9999>
DocumentRoot /var/www/app1/web
<Directory /var/www/app1/web>
</Directory>
</VirtualHost>
<VirtualHost *:8888>
DocumentRoot /var/www/app2/web
<Directory /var/www/app2/web>
</Directory>
</VirtualHost>
That works perfectly as http://myserver/app1/ is redirected to http://myserver:9999 and http://myserver/app2/ is redirected to http://myserver:8888 (the URL in browser changes). I want the URL in web browser to stay as http://myserver/app1 or http://myserver/app2.
I can do this by adding switch 'P' to RewriteRule, like so:
RewriteRule ^ http://%{HTTP_HOST}:9999/%1 [L,P]
This however creates problem, because now Symfony sees all requests to be coming from port 80 and redirects them back to that port not to the proper virtual host. I think one way to fix this issue would be to force Symfony2 to prepend all requests with the proper application name.
I tried to do so by modifying generateUrl method in vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php to always add application name in front of generated URL and it worked but only for page requests (assets were missing for example).
So my question is if there is a way to full Symfony into thinking that all requests should go back to proper 'app' URL?
My suggestion is that you don't try to do this via Apache configuration. You can configure each Symfony application to live in a subfolder, see:
http://www.yegods.it/2015/01/30/install-symfony-app-in-a-subfolder-of-an-existing-site/
You only need to edit app.php, app_dev.php and composer.json (for assets).

.htaccess rewrite [virtual] subdomain to [virtual] folder

I have a site with virtual pages where any request is rewrote to the index page in this way:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
I.e, http://www.example.com/job/edit is rewrote as http://www.example.com/index.php/job/edit where I parse it. That works great.
Now, I need to process every virtual subdomain in order that i.e. http://user1.example.com/job/edit becomes http://www.example.com/index.php/user1/job/edit.
I added in CPanel a wildcard subdomain (*.example.com), so every subdomain is pointed to the domain folder (without it I got 'Server not found' on every subdomain).
I tried every example that I found here, but I can't get them to work. If I print out $_SERVER['REQUEST_URI'] on the index page, I allways get the last part, but not the subdomain (i.e. /job/edit).
Is there any way of doing this?
Thanks in advance.
Edit: I already have a workaround for doing it. The $_SERVER['HTTP_HOST'] var contains the whole domain, i.e. user1.example.com. I can parse it in PHP and extract the subdomain, but I think it should be a more elegant way of doing it.
I have Done Same Like this.
Create a Folder called "user"
Create A index.php to handle The calls To user functions
Add A Virtual host in the Httpd.conf That Route The
*.example.com to the Path /public_html/user
<VirtualHost 0.0.0.0>
ServerAlias *.example.com
ServerAdmin webmaster#yourdomain.com
DocumentRoot /home/yourdoma/public_html/user
ServerName yourdomain.com
User yourdoma
Group yourdoma
BytesLog /usr/local/apache/domlogs/yourdomain.com-bytes_log
CustomLog /usr/local/apache/domlogs/yourdomain.com combined
ScriptAlias /cgi-bin/ /home/yourdoma/public_html/joe/cgi-bin/
</VirtualHost>
Now all Call will route to user Folder.
Add a Htaccess file in user folder to rewrite the base path

Issue getting CakePHP to work on MAMP in virtual host

I dont know how to get CakePHP to run in a virtual host on my local machine. I can get the code to work on the default http://localhost:8888/caketest/ but cant get it to work from http://cakeapp.local:8888/ where I get the warning
"URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don't / can't use URL rewriting"
In MAMP's httpd.conf I added
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "/Users/me/Documents/path/to/root/root"
ServerName cakeapp.local
</VirtualHost>
I saw in the help page that it expects
Options FollowSymLinks
AllowOverride All
# Order deny,allow
# Deny from all
but this crashed MAMP
both are CakePHP 2.4.9 with no changes to the default download
Edit: The not working files in /Users/me/Documents/path/to/root/root which dont work were copied to /Applications/MAMP/htdocs where they then worked, so it is an issue with the virtual host
Edit 2:
I thought I had found a solution but It looks like I was wrong. I changed the virtual host to point directly to /app/webroot which has made the default page show correctly, I then proceeded to add the DebugKit plugin (which I have working in a different local host file and the rewrites for its files still dont work.
For example app/Plugin/DebugKit/webroot/css/debug_toolbar.css gets loaded on http://localhost:8888/caketest/debug_kit/css/debug_toolbar.css but the file in the virtual host http://cakeapp.local:8888/debug_kit/css/debug_toolbar.css returns a 404 error (the plugin works), the files just dont get loaded.
If you copied the code from /Application/MAMP/htdocs to /Users/me/Documents/path/to/root/root, did the .htaccess file get copied across as well? This hidden file is often left behind.
OK, I think this works, the last time I tried to fix the directory it was not pointing at the correct directory.
I added the following to the httpd.conf:
<Directory "/Users/me/Documents/path/to/root/root">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
replace with this in your .htaccess file , it worked for me.
RewriteEngine On
RewriteBase /path/to/cake/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Htaccess Rewrite to PHP File in Parent Directory

I use a custom framework which has many sub applications. I want to assign a domain to each application folder but ran into an issue.
Apache vhosts config
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
</VirtualHost>
/var/www/framework/.htaccess
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
/var/www/framework/index.php
<?php
exit('WORKED!');
//Route request
//...
Everything's works fine except that it tries to use the index.php file in the document root such as "/var/www/framework/public/app1/index.php" when actually I want to use the index file "/var/www/framework/index.php".
So how would I get it to use the index.php file two directories above or relative to the .htaccess location?
You don't. The .htaccess will only be read starting at the root folder of the virtualhost.
You either need to clone the file (ugly unless you're using something like a source code repository to pull it as an external) or add it to your vhosts config. I'd recommend just making one 'rewrite.conf' file and loading it through the vhosts config through include.
vhosts conf:
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
Include rewrite.conf
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
Include rewrite.conf
</VirtualHost>
rewrite.conf:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
This does have a few drawbacks:
You'll need to reset the server every time you change it.
If you have a lot of rewrites, you'd probably want to keep this somewhere at the code level... however, I'd argue that you probably want a different solution (as in, a PHP handler) if you have a whole mess of rewrites.
Note that you could also just do the same rewrite directly in the .htaccess, but if you're going to do it, lower level seems better.
What you're doing is essentially a multi-site application.
You should set the document root to the folder that contains the main index.php file. Then that should route the request to the required app by checking the HTTP host property in the $_SERVER superglobal, like for example:
switch ($_SERVER['HTTP_HOST']) {
case 'app1.com':
include 'app1/index.php';
// Do stuff
break;
case 'app2.com':
include 'app2/index.php';
// Do stuff
break;
}

Categories