how to create route in laravel for the below 2nd option....
http://localhost:8048/
http://localhost:8048/content/645668/nice-up-civic-poll.html
1st it redirects to home page which is correct for me.
2nd I need to get what ever comes after 8048/
so basically content/645668/nice-up-civic-poll.html is a parameter which I need to deal with it separately and its dynamic link.
Route api in laravel :
Route::get('/', 'HomeController#index');
www.example.com will load home page with all stories.
The below links as an example should get the value after www.example.com/ basically its a story/article link so when that comes specific story will be displayed.
www.example.com/content/645668/nice-up-civic-poll.html
www.example.com/content/283206/something-here.html
www.example.com/content/234323/good-nice.html
www.example.com/content/451425/breakup-actor.html
www.example.com/content/365412/accident-occured.html
So basically get everything after domain name which is using apache server.
.htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want a home route and then every other URI to go to a single Controller method you can make a catch-all route:
Route::get('{catch}', 'SomeController#action')->where('catch', '.*');
This would catch any URI that doesn't match any previously defined route.
If you want everything to go to one place:
Route::get('{catch?}', ....)->where(...); // optional param
Post about creating a catch all route, answer using regex conditions on parameter:
SO - How do I make a Catch-All Route in Laravel 5.2
Update:
If these URIs you need to catch all have the same format,
www.example.com/content/645668/nice-up-civic-poll.html
you can register a route to match that format instead of catching everything possible:
Route::get('content/{id}/{slug}', function ($id, $slug) {
...
});
try using $request helper..
$leftbehind = str_replace($request->getHost(),'', $request->fullUrl());
or try this..
$request->getRequestUri();
Use $request->path()
The path method returns the request's path information. So, if the incoming request is targeted at http://example.com/foo/bar, the path method will return foo/bar
https://laravel.com/docs/5.5/requests#request-path-and-method
You can use The Request::path() to get the current url.
https://laravel.com/api/5.5/Illuminate/Http/Request.html - Check this for all the options available for Request.
eg: If you want to just check whether the users is in some url or not use this - Request::is('/url') // This will return true or false
You can use php's built in function, parse_url to retrieve content/645668/nice-up-civic-poll.html.
parse_url($url, PHP_URL_PATH)
Related
On my application i have a webshop with multiple items. For each item you are able to have different kind of colors. So the colors have to be in the url to send it as a parameter.
The moment you click on an item, you will get send to that item's page with the url http://cmsproject.local/item/2/nocolor. The nocolor is the thing i want to hide from the url, which would result looking something like this:
http://cmsproject.local/item/2.
If you have selected a color the last part of the url looks this "/2136c7". This is not a problem and should still be looking like this. Only when there is no color selected, the url should hide the nocolor part.
I have tried multiple forums, but none of them worked out for me. I have tried something like this:
RewriteRule ^2/nocolor/(.*)$ $1. and this
RewriteRule ^/2/nocolor [L].
My htaccess file looks like this:
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^/2/nocolor [L]
With this I expected that the url would "hide" the nocolor part, instead nothing changed and it didn't displayed any error.
If I understand your requirements correctly, I don't think you necessarily need to be handling this at the HTTP server level. From what I understand, you want a URL which can have an optional parameter as part of it.
Based on your example, my assumption is that the URL (with or without a color present) is handled by the same endpoint. So my suggestion is that you delegate that responsibility to the Laravel Router via optional route parameters.
Here's an example of how you could handle that, showcased with a simple route closure (this can of course be a controller action method):
Route::get('item/{id}/{color?}', function ($id, $color = null) {
// If the URL is "/items/2" then $color will be null
// If the URL is "/items/2/2136c7" then $color will be '2136c7'
});
Using the example above you can handle both situations.
You should do this in your laravel app, but in htaccess would the solution look like this:
RewriteRule ^/item/([0-9]+)/? /item/$1/nocolor
^ start of the match
? optional
+ at least one occurence
Take look at RegEx for further uderstanding.
I need to set the redirect in htaccess.
My htaccess is given below
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . frontend/web/index.php
So it redirects all requests to frontend/web/index.php
But the problem is that Suppose the following request comes /site/about
Currently using the above rules frontend/web/index.php is not receiving the path parameters. ie. /site/about is redirecting to
I need the path parameters as path parameters itself in frontend/web/index.php
.ie. /site/about should be mapped to /frontend/web/index.php/site/about
Also the path portion is dynamic. ie it can be anything like site/service
How can I do that rule in htaccess. I have checked some other solutions but most of them says to map path parameters to query strings which I cannot do as per my site settings.
You'll need to append the original path to the new one:
RewriteRule (.*) frontend/web/index.php/$1
As #Rickdenhaan said, You need to append the request path to your destination url. You can appened it using %{REQUEST_URI} variable or using a backrefrence at the end of your rewrite target.
Change your RewriteRule line to this :
RewriteRule . frontend/web/index.php%{REQUEST_URI} [L]
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.
I'm looking for a way to modify a request URL before it gets dispatched. For instance, the following URLs should be handled by the same controller/action:
/en/paris
/de/paris
/paris
I would like to capture the country code if it is present, then rewrite the URL without it so that controllers don't have to deal with it. I tried the 'dispatch:beforeDispatchLoop' event but it doesn't seam to be designed for that.
Any idea?
If you can convention that all country code comes first in the path, perhaps an additional rewrite rule can help you:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/(.*)$ index.php?_lang=$1&_url=/$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
EDIT
If you really need to do this in PHP, I'd recommend you to intercept the country code at the earliest to not break the default routing behavior (i.e need to write all routes manually). One way to do this is by setting a shared service in your main DI to replace the default 'router' service. The customized router consist simply in a child of Phalcon\Mvc\Router with the method getRewriteUri overridden by something that does whatever you want them just return the URI without the country code part:
namespace MyApp\Services;
use Phalcon\Mvc\Router as PhRouter;
class Router extends PhRouter
{
public function getRewriteUri()
{
$originalUri = parent::getRewriteUri();
// Now you can:
// Check if a country code has been sent and extract it
// Store locale configurations to be used later
// Remove the country code from the URI
return $newUri;
}
}
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){ ... })