How to have an exception to URL routing in CodeIgniter - codeigniter

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.

Related

How to remove id from url in laravel

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.

.htaccess MOD_REWRITE specific cases

I am really struggling with .htaccess since I can't figure out how the syntax actually works and what the commands are. Let me describe what I need to achieve and maybe someone could guide me to the right answer:
I have a website which works with JQuery Ajax. So there is one single index.php file in the root folder. Any request of a sub-page via internal link(mysite.com/contact.php) is going through Ajax and the content information gets loaded from a folder called "/pages/" to the index.php.
But if a user enters the url itself (mysite.com/content.php) Everything breaks since the directory actually doesn't exist. Remember the content files are inside /pages/. And a direct access to the raw content files would not display the site, but only the information in raw form.
To solve this I started using .htaccess to pass not existing directory request to php, which passes it to Ajax. But here I got stuck. I am really bad with .htacces. This is what I got from the internet:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works for the most part, except if the request goes through a subfolder (mysite.com/download/version1)
So here is what I need .htaccess to do:
Don't care if it is a folder or a file request, pass it to php.
(optional) Do not allow direct access to /pages/ folder, but do allow if request comes via Ajax
Do not mess up request calls for resource files. (This is important. When I fiddled around, I got many errors since .htaccess was also interfearing in resource file (.css/.js/.png) calls)
Feel free to take a look at the website trough: GitHub
The php part is inside index.php. The Ajax is done in PageHandler.js

unable to call different class after index in codeigniter

I just started learning codeigniter and came up with some problem I could not sort.
In routing the default controller = 'home'
the base_url()=localhost/CodeIgniter_2.1.3/:
So when the site is loaded the site_url()=localhost/CodeIgniter_2.1.3/index.php
Home controller contains a link , to register controller.
So when this register controller is inside home object, and when the is linked to localhost/CodeIgniter_2.1.3/index.php/register it works fine.
But I want to make my Register controller a different object so that, I can go to register page like this localhost/CodeIgniter_2.1.3/register which I cannot get done. I tried messing with 'routes' but no luck there. Any Ideas?
create in your root directory a .htaccess file with following rules to use URI segments on routing properly :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
more at codeigniter documentation

.htacces redirect for AJAX search engine indexing

Ok, so I made a pure html/javascript AJAX website, but I want my pages indexable by Google.
I have my content files with meta information in plain html, but without menubar etc. and I have my index.htm with all the menubars, javascript AJAX stuff, etc.
To make AJAX indexable for google, my URL's should look like "<something>#!<somthingelse>", which Google indexbot will change to "<something>?_escaped_fragment_=<somethingelse>", such that my server knows it should return the content directly, instead of the page that loads it via AJAX.
However, since my server is stupid, as it doesn't use server side processing, I need to perform a trick via htaccess (which is where I fail :( )
The idea is as following:
I have my fancy URL's http://mysite.com/page1#!1, http://mysite.com/page2#!1, etc.
Normally, htaccess should rewrite that to /index.htm?page=page1 such that my AJAX reads the URL param and automagically loads page1.htm content file.
For Google indexer, it should ignore this rewrite for any url containing "?_escaped_fragment_=1" such that the url points to the content page directly
This way I have to make a small compromise by putting #!1 in every fancy URL, but as far as I can think of, it is the only way to do this without server side processing (except for htaccess of course)
I just cant seem to get the rewrite rules to do this.
Here's what ive come up with so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)_escaped_fragment_=1(&.*|$)
RewriteRule ^(.*)$ %1 [L,R=301]
RewriteRule ^(.*)$ /index.htm?page=%1 [L,R=301]

creating admin folder inside CodeIgniter App

I have the front end fully working in a codeIgniter application. Now, I have to create admin as well. So, How would I create the admin section creating a new directory. Without interrupting codeigniter directory structure.
localhost/myapp/admin
CodeIgniter already supports 1 subfolder level within the controllers folder. So within /applications/controllers/ you can just add /applications/controllers/admin/ and it will work fine.
you could omit it out via .htaccess so that the directory actually works like a directory rather than how its initially developed to work.
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
what this does is turn on apache modrewrite then tells apache any calls to index.php, robots.txt or yourdomain.com/admin/ (and any sub-folders/files within) treat as you would normally without codeigniter mucking up the works. Also this will remove the required index.php from the URL you will be able to link to your site like
mydomain.com/home/
instead of mydomain.com/index.php/home/
There's an explanation about that in CI User Guide: Managing your Applications

Resources