I'm migrating my codeigniter files from apache to nginx, and looking at the rewrites for nginx. I came across the following rewrite to remove the codeigniter index.php
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
The rewrite works. But, I quite don't understand the /index.php?/ part.
UPDATE :
I tried the rewrite without the question mark, like rewrite ^/(.*)$ /index.php/$1 last;. It works for normal controllers, but breaks when a value is posted to a controller by third parties like fb oauth like /controller?code=something.
My questions
1) What is the role of the question mark in this rewrite ? Does this make index.php optional ?
2) Also, codeigniter does not route url's with question marks, like in a get request. For example, a get request like this
http://example.com/controller?code=somecodehere
breaks and does not call the controller, but trying to literally call controller?code=somecodehere. This routing takes care of that, but I just don't understand how.
Any insights on this rewrite will be helpful.
Thanks.
it's not a part of regexp but simply a question mark in rewritten url
so
abc
becomes:
/index.php ? /abc
as you should know - get parameters in php are passed after question mark sign in url
now codeigniter in index.php can read from $_SERVER['QUERY_STRING'] and will get /abc. From this it can load the proper controller
Related
I am trying to remove id from the url. My urls's are something like this,
https://www.example.com/p/29/web-test
But, I want the url like
https://www.example.com/p/web-test
And my route is,
Route::get('/p/{id}/{any}','TestController#dynamic_page')->name('dynamic.page');
I have tried several codes .htacces like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ p/$1 [L]
But its not working, can anyone please help me out, I am poorly trapped in it,
Try this
Encrypt the $id in blade
{{ route('route_name', encrypt($id)) }}
And then decrypt id in crontroller.
$id = decrypt($id)
It will display encrypted id in URL.
You can't do what you are trying to achieve because if you remove the id from the url, using a .htaccess file or any other redirection method, the final route will still hit Laravel at the end.
If this route does not contain the required data (in your case, the id), it will simply not work.
The only way to "hide" the id from the url while still getting it on the server side (in Laravel) would be to change it after the page has been loaded, using javascript. But to be honnest it is ugly and I strongly discourage you of doing this.
The good news is, putting the id in the url is not a bad practice. Doing what has been proposed in the comments, https://www.example.com/p/web-test-29, is totally fine. Also, using a unique slug could also do the job (and this time, without the id).
It's all up to you, both these methods are technically OK and SEO friendly.
As a side note, since you mentioned it, I would advice against the use of .htaccess files whenever it is possible, since it'll bind your application to Apache and you might run into troubles if you want to switch to another web server later, like Nginx.
Many people post that following htaccess works for removing "public" from url. But i am working on WAMP and this is not working:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
This shows following Error message from Laravel framework:
Sorry, the page you are looking for could not be found. 1/1 NotFoundHttpException in RouteCollection.php line 161:
There are other methods to remove "public" from url but I want to understand why this method is not working on my side which people followed frequently in their posts. my url is : http://localhost/laravel/ (directory: d:/wamp/www/laravel)
Can any one help me to understand why this popular method is not working on my side? Is their any issue in Laravel internal routing as I think my htaccess is working properly?
Thanks a lot in Advance.
I am sorry this is not exactly what you wanted but... Instead of rewriting the path, go to your Apache config and change your directory so it includes /public as well. That way you will get rid of the now necessary public from the URL. I know it is not an answer to your question, so please do not downvote. Nevertheless, it should solve your problem.
Your router is not picking up the redirected path because your laravel installation is a subdirectory within your webserver and the RewriteRule ^(.*)$ is picking up the whole path which includes /laravel. This is being appended to public/ because it's captured in $1.
The simplest case for you may be to alter the RewriteRule to not capture the laravel directory.
eg:
ReqriteRule ^laravel/(.*)$ public/$1 [L]
You may or may not need a forward slash ^/laravel/(.*)$ ...
Alternatively, (and more correctly) you should setup a virtual host within your apache configuration to serve the laravel application in the root of a virtual host (as it's designed) or you might be better off in the long run using Laravel Homestead as a preconfigured development environment.
I have a slight issue. I have configured my default controller as :
c$route['default_controller'] = 'news';
so when I point my browser to http://localhost/mysite all the news get loaded as instructed by the news controller.
Now on this page, I have given links to "read article" to read the full article. For this I have to point my browser to http://localhost/index.php/news/view/news-slug. I want to remove index.php from the url. I had already tried re-routing using wildcard without any success. Can you tell me how to do that?
There is a documentation on removing the index.php from the URL if you are using Apache with it's mod_rewrite:
Removing the index.php file
Using the following rewrite code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also, there are many similar questions to yours, have a look at these questions from the search:
Search query on removing index.php in Codeigniter
You need to define your .htaccess file for this. Check this wiki: http://codeigniter.com/wiki/mod_rewrite
i'm trying to use iirf for what looks like asimple rewrite but it's not working.
what i need:
rewrite http://www.mydomain.com/ru to : http://www.mydomain.com?page=russian
the objective being that a get param would be sent but the user would see the first url on their browser's url bar.
i'm using the following code:
RewriteEngine ON
StatusUrl /iirfStatus
RewriteRule http://www.mydomain.com/ru http://www.mydomain.com?page=russian
does this go (the iirf file) on the site's root or in a 'ru' subfolder (tried both)?
what am i doing wrong or missing?
thanx and have a nice day :-)
The following should work:
RewriteRule ^/ru$ /?page=russian [I,L]
You should put this in the iirf.ini file in the web sites root folder.
Check http://www.mydomain.com/iirfStatus to see whether iirf was able to read your configuration file.
Also, you may use RewriteLogLevel with a value of 2 or 3 and RewriteLog to see whether the incoming url was rewritten, and how (or why not).
I have some legacy code that needs to be used in a new application. I would like to write the new application in CodeIgniter, but I still need to have some way of accessing the old code. what I would like to do is have an exception in the routing so that any url that has the format of example.com/old_stuff/* goes to an old_stuff folder, and acts as a regular, un-routed applications, while any other url such as example.com/new_stuff would route to a new_stuff controller, for example. So essentially what I want it to have URLs behave as they usually would in CodeIgniter, with the exception of any that start with one certain string.
What's the best way to accomplish this?
place codeigniter at your web root, and have your folder old_stuff in the web root also.
then use .htaccess with these rules (assuming you have mod_rewrite)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|old_stuff|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
then, a uri beginning with old_stuff will just serve up the content bypassing codeigniter.