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.
Related
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)
I am trying to build a custom mvc framework in php and its folder structure is something like this in my localhost.
Controllers(Dir)
Models(Dir)
Views(Dir)
Lib(Dir)
Config(Dir)
Lang(Dir)
Public
.htaccess
In this .htaccess file I am redirecting all the request in the public folder and the code in this .htaccess file is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And the following is my public folder contents
css(Dir)
js(Dir)
images(Dir)
mod(Dir)
index.php
.htaccess
In this htaccess I am checking if the requested uri is not the folder or file in this folder then pass that request to index.php, which in turn does the url processing for controllers and actions etc. And its code is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %[REQUEST_FILENAME] !-f
RewriteCond %[REQUEST_FILENAME] !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Everything is working fine except when I try to use any css file,js, image or any php file in mod directory(I am calling those file via ajax) it still pass that request to index.php which again try to locate them as a controller etc.
So please can anyone tell me what I am doing wrong and how can I solve this problem. I still don't fully understand Rewrite Rules and Conditions so its possible that I have done something wrong in my rules.
Thank you,
Anchal Bhargava
I tried to search and use the solutions already posted but nothing worked for now and I tried pretty much everything I could but didn't work.
These are the app directories:
localhost/
root/
data/
app/
public/
index.php
.htaccess
The htaccess is this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /root/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
The first problem is that if I go to localhost/root/public the website works as intended but in the url I still see 'public' which I'd like to remove.
The second problem is that my objective is to go to the index from the url localhost/root/ but now, if I go there it just shows the files in the folder.
Thank you.
As requested, these are the routes that I use:
$app->get('/', function () use ($app) {
// code
})->bind('home');
$app->get('/about-me', function () use ($app) {
// code
})->bind('about-me');
There are two mistakes in your system.
First:
The .htaccess located in the root folder should be used to remove the 'public' part of the URL. The code for that is like the following:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
It basically grabs every route and redirects it to the public folder, exactly as you wished!
Second:
You should create another .htaccess file in your '/public' folder to redirect everything to the index.php file. The code for that should look like this:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
You should end up with the following structure:
localhost/
root/
.htaccess (removes 'public')
data/
app/
public/
index.php
.htaccess (forward to index.php)
This should solve both your problems. If it does, please mark the question as completed by marking my answer as the one that solved it. If it does not, please comment on other problems that occur to you!
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/
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