Kalpesh Rajai
Kalpesh Rajai

Reputation: 2056

Silex Routing Issue - Remove page name and directory name

First of all, I am new in Silex PHP Framework and i am trying to create one RESTApi for my Android app.

My Directory Structure

abc
----vendor
----web
--------index.php
--------.htaccess
----composer.json
----composer.lock

my Index.php File coding

<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

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

$app->get('/', function () {
    return 'Hello!';
});

$app->run();

my .htaccess file coding

RedirectMatch permanent ^/index\.php/(.*) /$1
<IfModule mod_rewrite.c>
    Options -MultiViews    
    RewriteEngine On
    RewriteBase /abc/web/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?!web/).*)$ web/$1 [NC,L]
</IfModule>

When i hit this URL http://127.0.0.1/abc/web/

it's work perfectly good and get response Hello!

but when i hit URL like this http://127.0.0.1/abc/web/hello/123

so i got Error like this

Not Found

The requested URL /abc/web/hello/123 was not found on this server.

And if i hit this URL http://127.0.0.1/abc/web/index.php/hello/123 so it's work good and i got response OK. Hello 123

So, MY QUESTION is how i remove page name index.php and dir name web from my URL and i want my URL to look like this http://127.0.0.1/abc/hello/123

Is that possible ? and how ?

Please help, Thanks in advance.

Upvotes: 1

Views: 163

Answers (1)

user4545769
user4545769

Reputation:

So what's happening is that your rewrite rule isn't sending requests through the Silex front controller (index.php) which is required for getting friendly routing working. I'd strongly suggest reading up on the routing documentation as well as the example Apache configuration.

To sort this, the following may work for your .htaccess:

    RewriteRule ^((?!web/).*)$ web/index.php/$1 [NC,L]

I'm not entirely sure what the ^((?!web/).*)$ regex is meant to achieve, ideally it would be:

    RewriteRule ^ index.php [QSA,L]

As per the documentation. Your requirements though may dictate you fiddling with this until it works though.

Upvotes: 1

Related Questions