My .htaccess:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /silex/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>

Index.php:

<?php
ini_set('display_errors', 1);
require_once __DIR__.'../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function ($name) use ($app) {
return 'Hello '.$app->escape($name);
});

$app->run();

I got this error when accessing localhost/silex/hello/world:

"Sorry, the page you are looking for could not be found."

why?

有帮助吗?

解决方案

Try this in your .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /silex/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

Location of files should be:

/silex/.htaccess
/silex/web/index.php

其他提示

here is the silex complete path

/var/www/html/silex

".htaccess" file

/var/www/html/silex/.htaccess

<IfModule mod_rewrite.c>
Options -MultiViews

RewriteEngine On
RewriteBase /silex/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

"index.php" file

/var/www/html/silex/web/index.php

require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->get('/', function () {
    return 'Index Page';
});
$app->get('/hello', function () {
    return 'Hello Page';
});
$app->run();

.htaccess should be

<IfModule mod_rewrite.c>
Options -MultiViews

RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>

VirtualHost file must looks like this

<VirtualHost *:80>
ServerName site.com
ServerAlias www.site.com
DocumentRoot /var/www/html/silex/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And everithing works like a charm! ;-)

".htaccess" located at /var/www/html/silex/

and Silex application bundle

placed at /var/www/html/silex/web/

You can try adding:

RewriteCond %{REQUEST_FILENAME} !-d.

It worked for me.

This might work:

<IfModule mod_rewrite.c> 
      Options -MultiViews 
      RewriteEngine On 
      RewriteBase /silex/web 
      RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteRule ^ index.php [QSA,L] 
</IfModule>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top