laravel 4.2 route with trailing '/' - laravel

we have a REST Api, that declares the following routes for entity:
Route::get('entities', 'Api2EntityController#index');
Route::get('entities/{entityId}', 'Api2EntityController#show');
Route::post('entities', 'Api2EntityController#store');
Route::put('entities/{entityId}', 'Api2EntityController#update');
Route::delete('entities/{entityId}', 'Api2EntityController#destroy');
POSTing data to entities would correctly call Api2EntityController#store.
But the same request to entities/ (has trailing '/') would call Api2EntityController#index.
we have tried adding a new route for POST or using regex or changing the order of the routes, still we have no solution so far on how to accept the POST on the URL with trailing '/'

There is a rewrite active to remove any trailing slashes.
Check your public/.htaccess file
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

Related

Laravel trailing slash connects to GET instead of POST

I have a POST route setup called user/connect which points to the relevant controller function ( which expects post varialbles), but adding a trailing slash e.g. user/connect/ redirects it to GET thus returning "MethodNotAllowedHttpException"
I am unable to remove the forward slash ( as the consuming app is sending it and i have no control ) . Any pointers on how i could get the route with the trailing slash to also point to my POST route ?
You should use ‘php artisan list:route’ to find a conflicting route. Laravel will not alter the HTTP method. The app could also be wrong so some investigation is needed there as well.
Fixed using user3532758 comment, seems that if i remove the following from the public/.htaccess it fixes it, but i am not sure if it opens up another security hole so checking that as well
removing the following fixes it:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

What's up with this simple mod-rewrite rule?

As part of Google's approach to crawling AJAX-populated content, I've got the following in a .htaccess file:
RewriteEngine on
#snapshot requests
RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]+)
RewriteRule ^.*$ system/snapshot_mode.php?project=%1
I trigger the rule with a URL like
http://myserver.co.uk/?_escaped_fragment_=token
...but it fails to go to the page specified. It does, however, if I remove the query string part of the redirect, i.e. change it to
RewriteRule ^.*$ system/snapshot_mode.php
What's up with that?
[EDIT]
I added a [R-302] to the rule and it now tries to redirect me to the script BUT via a mangled, local machine filepath, i.e.
http://localhost/C:/xampp/htdocs/docula/system/snapshot_mode.php?project=token
...and gives me a 403 access denied error.
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]+)
RewriteRule ^.*$ system/snapshot_mode.php?project=%1 [L,QSA]
RewriteRules and Query String:
By default, a RewriteRule does not change the query string. Therefore, QueryString is not required in the RewriteRule. If you are not adding a parameter to the replacement URL, RewriteRule passes on the incoming query string unchanged.
If you are adding new parameters, the original query string will be discarded and the rewritten URL will get only the new parameters.
If you want to combine the original query string and the new parameters, you can append it using the Query String Append [QSA] flag.
In your code, you have a new parameter project=%1 without the [QSA] flag and therefore the _escaped_fragment_=([^&]+) parameter is discarded, making the RewriteCondition fail.
Also Note:
[R-302] is incorrect syntax. It should be [R=302]
[QSD] - Query String Discard flag is not something we would want in this case.

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

rewriting URLs from .json to .php

I want http://server/path/app.json?a=foo&b=bar to map to http://server/path/foo.php?a=foo&b=bar using mod_rewrite. I have the following incantation in my .htaccess which doesn't give any joy
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Suggestions?
Update: Adding rewrite.log and error.log output (comments don't allow formatting)
I get the following in the rewrite.log
strip per-dir prefix: /Users/user/Sites/dir/app.json -> app.json
applying pattern '^([^.?]+).json(.*)$' to uri 'app.json'
rewrite 'app.json' -> 'app.php'
add per-dir prefix: app.php -> /Users/user/Sites/dir/app.php
internal redirect with /Users/user/Sites/dir/app.php [INTERNAL REDIRECT]
and the apache server log says
The requested URL /Users/user/Sites/dir/app.php was not found on this server.
If I read your question correctly you want:
http://server/path/app.json?a=foo&b=bar
Going to:
http://server/path/foo.php?a=foo&b=bar
Sowhen you capture (app).json $1 is app and $2 is your second parenthesis, it's ... nothing (the part between json and the ?). As everything after the question mark is the QUERY STRING and cannot be captured here. Your rewriteRule is working on the requested file, not on the QUERY STRING. So you didn't captured foo anywhere. For the QUERY_STRING you could use the [QSA] flag on the rewriteRule, that would simply append a=foo&b=bar after your rewrite.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Here you tell apache to reuse $1 (the filename without .json), so app.json will get redirected to app.php, not foo.php.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php [L,QSA]
Will redirect app.json?a=b&z=r to app.php?a=b&z=r.
Now if you really need to capture foo as the first QUERY_STRING parameter the rule will become harder. But you could do it like that (here instead of the first parameter I detect the parameter 'a=' and capture his value in %4):
RewriteCond %{QUERY_STRING} (.*)(^|&|%26|%20)a(=|%3D)([^&]+)(.*)$
RewriteRule ^([^.?]+).json$ %4.php? [L,QSA]

always preserve URL when redirecting to subfolder

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*) /app/app_site$1 [L,PT]
This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.
However, when I enter 'http://localhost/app', following things happen:
the correct page is fetched, at /app/app_site/index.html
yet, the URL is redirected to 'http://localhost/app/app_site/'
I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!
This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.
The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$ /app/app_site/$1 [L,PT]
This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

Resources