Symfony: I cannot use a route name starting with "web" string - php

I've used a route name as "webmaster" (www.domain.com/webmaster) and symfony shows me an error like:
NotFoundHttpException: No route found for "GET master"
And my htacces is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web/app.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I know, "web" is reserved keyword in a Symfony2 Application. How i use this exception in my project.
Thanks.

Symfony's web directory should be your site's document root, and as such, does not need to be included in the path as the base of your rewrite rule.
So, assuming your .htaccess file is located at PROJECT_ROOT/web/.htaccess then all that should be required is this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
Again, this assumes that you've set up Apache to point to PROJECT_ROOT/web as the site's document root (where PROJECT_ROOT would be something like /var/www/vhosts/symfony_project)

Reacting to your comment
I don't have a static route as "webmaster". It's dynamically generated in a controller. For example: path: /{category}
I guess you need to add a prefix to your route.
In exemple, how can symfony know if www.myapp.com/admin is a route one of your dynamic route or a standard route?
You need to have at least a prefix like /site/{category}

Related

Slim Framework .htaccess forward

There are several questions out there to that topic, but not a single one did match my question, because I am using Slim Framework 3.
One of my routes, for example, is /stats
When I make requests to http://localhost/slim-app/public/stats - it works.
But, I want to make requests to http://localhost/slim-app/stats - and that doesn't work. Slim gives me the error
Page Not Found The page you are looking for could not be found. Check
the address bar to ensure your URL is spelled correctly. If all else
fails, you can visit our home page at the link below.
My folder structure looks like that:
/slim-app
.htaccess
/logs
/public
index.php
.htaccess
/src
routes.php etc
/templates
/tests
/vendor
The .htaccess- File inside the slim-app folder looks like that:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
The .htaccess- File inside the public- folder like that:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
What should I change inside the htaccess- files to make it work? Thank you guys!

Hide directory name in URL

I have been trying to hide a folder name from a URL and I'm failing miserably. My URL id 0.0.0.0/project/api/api/foo. As you can see there are two api's and I only want to show one. The .htaccess I have been working with resides on the root of the directory and looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/api/$1 [L]
</IfModule>
I've tried a couple different variations of this but nothing is working. The first api is a directory name and the second api is something Laravel puts in for a route and is not a directory. It's ugly to have both, so how can I remove the first api?
There is no need to mess with the .htaccess. You just need to update your routes file to not include the second api path.
You can either remove the leading api segment you've manually defined on your routes, or you can remove the automated api prefix that Laravel adds by default to all routes in the routes/api.php file.
To remove the default prefix, open your app/Providers/RouteServiceProvider.php file, go to the mapApiRoutes() method at the bottom, and remove the 'prefix' => 'api', line.
To make the server accept your requests you can use this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/api/(.*)$ /project/api/api/$1 [L]
</IfModule>
This rule will make every request to YOURDOMAIN/project/api/SOMETHING looks (in your server) as if the actual request was YOURDOMAIN/project/api/api/SOMETHING
However - node that you need to make sure that the links inside your website/application are correct.

Laravel 4.2 : Route is conflict with subfolder name inside public folder

I'm got some problem with Route in Laravel 4.2. Here is details:
I have an example route:
Route::get('users', function(){return 'some-thing'});
and the route still work normally at http:://localhost:8000/users.
However, when I create a subfolder inside public folder, its name is "users".
--public
--users
That link didn't work and return the folder index. I know that is a big sercurity problem.
How to fix it? Can you please help me?
I wouldn't say a huge security problem, if there is sensitive data in that folder, then it shouldn't be in "public". You could deny directory listings (assuming Apache virtualhost or .htaccess):
Options -Indexes
As in Laravel's (and most other frameworks') .htaccess file, it only uses the "Router" if the file or directory doesn't exist. This is common as shown below:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Can't access controller nore action in Zend Framework

In my Zend Framework project I have the following base URL
http://domain/~myFolder
If I access it like so, I can access the indexAction with no problem but if I try to access it explicitly like so:
http://domain/~myFolder/index
I can't, nore can I access different controllers or actions.
It always says
/public/index.php was not found on this server.
How can I access the different controllers and actions in my project? Do I need to add a route?
Change the contents of the .htaccess file according to...
From: http://framework.zend.com/manual/1.12/en/zend.application.quick-start.html
You may note that the application environment constant value looks for
an environment variable "APPLICATION_ENV". We recommend setting this
in your web server environment. In Apache, you can set this either in
your vhost definition, or in your .htaccess file. We recommend the
following contents for your public/.htaccess file:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I was able to access the controller page by putting the controller name after the index.php:
ie: yoursite/public/index.php/ControllerName
It worked in my case.

mod_rewrite to replace beginning of %{REQUEST_URI} for Slim Framework

I have a web directory structure like so:
root
/content
/plugins
/myplugin
/Slim (folder containing Slim Framework)
index.php
/other_folder_1
/other_folder_2
.htaccess
index.html
I'm interested in what to specify in my .htaccess file in order to refer to a directory that isn't actually present on the server, but actually point to the Slim app in the /myplugin directory.
Here are a few example URLs, which I'd like users (or myself) to be able to use in the browser's location bar, or link with in documents:
1. http://example.com/nonexistent_dir
2. http://example.com/nonexistent_dir/info
3. http://example.com/nonexistent_dir/info/details
I'm trying to rewrite these URLs to the following:
1. http://example.com/content/plugins/myplugin/index.php
2. http://example.com/content/plugins/myplugin/index.php/info
3. http://example.com/content/plugins/myplugin/index.php/info/details
...which would all actually be handled by the index.php Slim Framework app in the /myplugin directory. It's important the apparent URLs remain as they appear in the first example, without being changed in the location bar.
Here's what is currently in the .htaccess file in the root directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/schedule [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /content/plugins/myplugin/index.php [QSA,NC,L]
</IfModule>
This redirects all 3 of the test examples to http://example.com/nonexistent_dir, using the / route. So my thought is that I should be capturing everything after the nonexistent_dir, whether it be something or nothing, and appending it to the end of the RewriteRule somehow. But I don't understand how.
I realize that using parentheses around an expression will enable me to use the contents as a variable, referred to it with $1 (or $2, $3... for multiples), but I don't know how to apply it to this solution.
Any help will be most appreciated.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nonexistent_dir(/.*)?$ /content/plugins/myplugin/index.php$1 [L]
Slim actually discards the base directory, and sets $env['PATH_INFO'], taking the content of this variable to match against the specified routes.
For example, lets take a /sub/index.php (Slim index file) and this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^somedir(/.*)?$ /sub/index.php$1 [L]
...and this route specification:
$app->route('/info', function() use ($app) { ... });
So, with a GET request to /somedir/info, Slim strips /somedir from REQUEST_URI and sets $env['PATH_INFO'] with value /info (this is actually done in the constructor of \Slim\Environment class).
Later, the Router class will match /info and execute the closure function.
If you want to pass parameters via url, the route would be, for example:
$app->get('/info/:param', function($param) use ($app){ ... })

Categories