Silex, in symbollically liked subdirectory, does not route - php

I have a Silex project. Works on localhost(using php.exe) but I've just transferred it to a subdirectory of an existing website. For example:
www.website.foo/silex/
On the site, because of funky existing routing, the silex app is symbolically linked in the webroot under a /silex/ folder, but is actually elsewhere in the filesystem. The index page works.
I wasn't using an .htaccess file, but I copied the one from the documentation, but it hasn't gotten me anywhere.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /var/www/webroot/silex/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I am at a complete loss as to why it doesn't work, let alone what to change to fix it.
Edited .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /silex/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Question
Does the .htaccess file have to be in the root directory? Or is being in the application directory fine?

Your RewriteBase directive is wrong, its related to web root, not your filesystem structure, so just use RewriteBase /silex/

Related

RewriteRule not working with the given parameter

I did some research and spent my last 2 days trying to make my .htaccess work without success, and I can't fully understand how .htaccess really work.
This is the URL I'm trying to rewrite, using GET in my .php files:
http://localhost/BDsite/tables/table.php?table=Energy
and I want it to be like this:
http://localhost/BDsite/tables/Energy
Well, this is how my .htaccess is written, and its located inside the site folder, which is /BDsite
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^BDsite/tables/([^/]*)$ /BDsite/tables/table.php?table=$1 [L]
</IfModule>
Sadly nothing is happening with my URL.
As opposed to per-server rewrites, it is possible to do rewriting inside sections or .htaccess files at the expense of some additional complexity. This technique is called per-directory rewrites.
The main difference with per-server rewrites is that the path prefix of the directory containing the .htaccess file is stripped before matching in the RewriteRule.
A RewriteBase should be used to assure the request is properly mapped.
(source: apache.org - rewrite intro .htaccess files)
So, using your directory structure, and adding the necessary RewriteBase you'd get the following that should work:
.htaccess file in root folder (BDsite)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /BDsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tables/([^/]*)$ tables/table.php?table=$1 [L]
</IfModule>
Project structure:
So now if you request: localhost/BDsite/tables/energy,
you'll get served localhost/BDsite/tables/table.php?table=energy
(check this by var_dump-ing $_GET in file table.php)

Cakephp 3.x .htaccess issue on bluehost server.

I want to setup cakephp 3.x solution inside WP website.
On my bluehost server, I have directory structure in public_html is as follows:
I want to access stagetribescrm directory and that directory contains Cakephp 3.x file structure.
.
Code Inside of public_html/stagetribescrm/.htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /stagetribescrm/
# RewriteRule ^$ webroot/ [L]
# RewriteRule (.*) webroot/$1 [L]
</IfModule>
Code Inside of public_html/stagetribescrm/webroot/.htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
And the problem with this code is that it couldn't load css and js files inside webroot folder's file.
Thanks.
Please check for rewrite module is turn on or not in bluehost server than
follow below link:
http://www.abhinavsood.com/install-cakephp-in-subdirectory-of-wordpress/

Htaccess for Silex

I'm trying Silex for first time but I can't get a simple call with a friendly URL to work properly, but if I use a non friendly URL it does it's job. I'm experimenting in localhost and the domain is
localhost/site
and the structure looks like this:
site
|- ...
|- .htaccess
|- /php/
|- rest.php
The .htaccess code:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rest/(.*)$ php/rest.php/$1 [QSA,L]
</IfModule>
If I try calling
localhost/site/php/rest.php/anything
It works properly and returns the expected value, but if I try this
localhost/site/rest/anything
It just keep saying
No route found for "GET /site/rest/anything"
When the route for it to work must be /anything
I have tried:
Moving .htaccess to /php/.
Changing the RewriteRule to RewriteRule ^ rest.php [QSA,L].
Changing the RewriteBase to RewriteBase /site/php/ and /site/.
Thank you.
I don't actually think this is possible, because Silex doesn't look for the /$1, and can only see what's in your address bar. (Unless the rest.php resides in the directory being requested.)
Your best bet would probably to reconsider your directory structure. If the web root for Silex is the php folder, then the request should be made to localhost/site/php/anything, with the .htaccess file being in that directory.
Alternatively, just rename the php folder to rest, and make sure you use the following rules (.htaccess file saved in the rest directory):
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /site/rest/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ rest.php [L]
</IfModule>
Lastly, you could just define you routes in Silex as site/rest/{anything} without changing your current setup. That is cumbersome, though, which leaves the previous option as the best option.

Silex Micro Framework: Run from a subfolder

I download Silex because I want to try it in my next project. I'm using mamp under Windows 8.1 64x , I think that this doesn't matter but, anyway, I put Silex into C:\mamp\htdocs\projectfolder\ (http://locahost/projectfolder/)
This means that I got http://locahost/projectfolder/vendor and http://locahost/projectfolder/web/
I wrote this .htaccess under "web" directory
Options -MultiViews
RewriteEngine On
RewriteBase /projectfolder/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
It works perfect if I come into http://locahost/projectfolder/web/hello, but I want to come from /projectfolder/, so I created a new htaccess file in /projectfolder with this content:
Options -MultiViews
RewriteEngine On
#RewriteBase /projectfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [L]
But when I run it, the app retun an NotFoundException: Sorry, the page you are looking for could not be found.
Thanks in advance
I'm new to silex too, so if anyone has better insights please tell/teach us :)
I decided to create a project on a separate folder from silex and, what I did was, I created a new index.php requiring silex's autoload.php and a new .htaccess which is basically the same.
So, I have this structure:
/myproject/silex/
/myproject/app/
/myproject/app/index.php
/myproject/app/.htaccess
my .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
and my index.php
<?php
require_once __DIR__.'/../silex/vendor/autoload.php';
...
BTW, I configured a vhost to point to /myproject/app/
Hope this helps.

Can't find resources.json

For some reason when I deploy my API using Restler's API Explorer (a fork of Swagger UI) to production it gives me a general 404 error when I load:
/api/explorer
When I am more explicit and state:
/api/explorer/index.html
It loads the framing for the page but then reports "404 : Not Found ../resources.json" in red text below the header:
I'm fairly certain there's something environmental flaring up as the same files locally work. I also checked the .htaccess file in the /api/explorer directory and it looks right to me:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
Any help would be appreciated.
It turns out all the problems were down to a mod_rewrite problem. I'm still not 100% on WHY this problem showed up in one environment but not others with precisely the same httpd.conf and .htaccess. Oh well, the solution is to be explicit in the rewrite rule about your base url using the RewriteBase directive.
In the API directory I now have the following .htaccess file:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In the API-Explorer directory I now have the following .htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api/explorer
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
In order to troubleshoot this problem one invaluable tip that I came across is turning on Apache's mod_rewrite logging.
# Adding logging for Rewrites
RewriteLog logs/rewrite.log
RewriteLogLevel 2
Put this in any globally scoped area of httpd.conf; I put it around the entries that were already there about logging but you can also just put it at the end if you like that better. In addition, the basics around rewrite troubleshooting includes (apologies if this basic):
make sure that your httpd.conf has AllowOverride All and Options FollowSymLinks set for the directories you serving Restler out of.
You can put all of the configuration into httpd.conf and avoid .htaccess files altogether (and it's a bit faster that way too) but if you do that remember that httpd.conf has absolute url referencing versus .htaccess's relative.
You can check the ReadMe file on the Restler Github page
https://github.com/Luracast/Restler-API-Explorer#readme
Did you add the 'Luracast\Restler\Resources' class to create resources.json at API Root?
In your own index.php, the addAPIClass section should look like this:
$r = new Restler();
$r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
// ... your own addApiClass
$r->handle();
It is in the documentation under "Use", but I had trouble figuring this out as well...
Make sure you have cache directory with write permission in your api root

Categories