I have a route like this
http://localhost:8000/produk/slug-product
I want to my url like this, remove produk in url
http://localhost:8000/slug-product
What should I do ?
Do not use .htaccess to handle this. Define route for slugs without any segment at the end of the routes list in your app:
Route::get('{slug}', 'FrontController#getBySlug');
So, all requests which are not related to any of the routes, will go to the getBySlug method:
public function getBySlug($slug)
{
$data = Model::findBySlug($slug);
....
}
I have problems with dealing ajax requests on my server.
I have this ajax request from js:
$.get('/fetchEntries',{id:id},function(data){
if(data){
alert('success');
}else{
alert('failed);
}
});
On my routes I got:
Route::get('/fetchEntries','EntriesController#fetchEntries');
My controller had:
public function fetchEntries(){
if(Request::ajax()){
$id= Input::get('id');
dd($id);
return Response::json($id);
}
}
This code is fully functional on my development machine in Windows and alerts 'success'.
But on my deployment server (Ubuntu) it alerts 'failed' which means it did not fetch any data. When I tried to debug the file on the server putting dd(Input::get('id)); or dd($id) these return NULL value even if I have the right path on inspect element>Network tab with fetchEntries?id=1.
I was really confused with this. It works fine on my Windows machine but on my server it doesn't.
Try this...
Route::get('/fetchEntries/{data}','EntriesController#fetchEntries');
public function fetchEntries($data=NULL){
if(Request::ajax()){
$id= $data;
dd($id);
return Response::json($id);
}
}
I believe the issue lies on your .htaccess file on the server. The RewriteRule is not properly done.
Find the similar line and fix as follows.
RewriteRule ^(.*)$ /index.php?/$1 [QSA]
[QSA] is the changes section
I cannot find info for redirecting as 301/302 in the Laravel docs.
In my routes.php file I use:
Route::get('foo', function(){
return Redirect::to('/bar');
});
Is this a 301 or 302 by default? Is there a way to set it manually? Any idea why this would be omitted from the docs?
Whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector class defines a $status = 302 as default value.
You can define the status code with the to() method:
Route::get('foo', function(){
return Redirect::to('/bar', 301);
});
I update the answer for Laravel 5!
Now you can find on docs redirect helper:
return redirect('/home');
return redirect()->route('route.name');
As usual.. whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector class defines a $status = 302 as default value (302 is a temporary redirect).
If you wish have a permanent URL redirection (HTTP response status code 301 Moved Permanently), you can define the status code with the redirect() function:
Route::get('foo', function(){
return redirect('/bar', 301);
});
martinstoeckli's answer is good for static urls, but for dynmaic urls you can use the following.
For Dynamic URLs
Route::get('foo/{id}', function($id){
return Redirect::to($id, 301);
});
Live Example (my use case)
Route::get('ifsc-code-of-{bank}', function($bank){
return Redirect::to($bank, 301);
});
This will redirect
http://swiftifsccode.com/ifsc-code-of-sbi to http://swiftifsccode.com/sbi
One more Example
Route::get('amp/ifsc-code-of-{bank}', function($bank){
return Redirect::to('amp/'.$bank, 301);
});
This will redirect http://amp/swiftifsccode.com/ifsc-code-of-sbi to http://amp/swiftifsccode.com/sbi
Laravel 301 and 302 redirects using redirect() and route()
301 (permanent):
return redirect(route('events.show', $slug), 301);
302 (temporary):
By default, Route::redirect returns a 302 status code.
return redirect()->route('events.show', $slug);
Offical Laravel Docs,'Redirect Routes': https://laravel.com/docs/5.8/routing#redirect-routes
You can define a direct redirect route rule like this:
Route::redirect('foo', '/bar', 301);
As of Laravel 5.8 you can specify Route::redirect:
Route::redirect('/here', '/there');
By default that will redirect with 302 HTTP status code, meaning temporary redirect. If the page is permanently moved you can specify 301 HTTP status code:
Route::permanentRedirect('/here', '/there');
/* OR */
Route::redirect('/here', '/there', 301);
Laravel docs: https://laravel.com/docs/5.8/routing#redirect-routes
I have a url like
"www.site.com/index.php/details/productname"
and I get 404 error on that link in codeigniter but the URL www.site.com/index.php/details/ is working fine. I want handle url parameter in seo manner.
You have to add /index/ segment in part of your url:
http://www.site.com/index.php/details/index/productname/
If you want to open url such like that http://www.site.com/index.php/details/productname/:
1) you need to define _remap() method:
public function _remap($method)
{
if ($method == 'index')
{
$this->viewDetails($this->uri->segment(2));
}
else
{
$this->default_method();
}
}
OR
2) use application/config/routes.php file
$route['details/(:any)'] = "products/viewDetails/$1";
From User-Guide:
routers.php
_remap method
I'm using Codeigniter 2.1.3 with Friendly URL (.htaccess).
In Controller:
public function confirm($key) {
var_dump($this->input->get());
}
But link _http://site.com/confirm/12345 returns "boolean false".
How to enable Query strings in URL, or how to filtering $key ?
My .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|files|templates)
RewriteRule ^(.*)$ /index.php?/$1 [L]
You receive key as the function argument, so you can do this:
public function confirm($key) {
echo $key;
}
http://site.com/confirm/12345 will echo 12345.
Filtering of characters in this can be done via $config['permitted_uri_chars'] in config.php.
If you want to receive it as a GET parameter and want to perform XSS filtering on it, the URL needs to be
http://site.com/confirm?key=12345 and in your controller
public function confirm() {
echo $this->input->get('key', TRUE); // true implies XSS filtering
}
The second method requires $config['enable_query_strings'] to be set to TRUE.