how to add index.php in codeigniter with every anchor - php

I have developed a shopping cart and have removed index.php using .htaccess file but now my client is asking me to show him the site on free web hosting first i have tried 000webhost but they do not support .htaccess file hence i moved to orgfree.com it also dont support .htaccess file than i remove .htaccess file and it created a problem because now i need to put index.php infront of every url but now i am consfuse that how can i add index.php with every achor doing this with every link manually will be a very huge process because the project is so big so there might be a solution for adding index.php to every anchor in codeigniter .
Here are some setting i have done so far please suggest me or help me adding index.php to every anchor
In Config.php
$config['index_page'] = 'index.php';
Before my anchor were like that
<li class="active">Home</li>
But after removing .htaccess file i need to put index.php with every anchor manually which is headace and i cant do it please show me the shortest path to add index.php with every link or anchor.
<li class="active">Home</li>

Both Nicolas and J Young are correct. you should use URL helper to avoid any trouble in the future. But since you want to a quick-fix, how about fixing link urls via JS
i'll give you a jquery example,
$(document).ready(function(e){
$('a').each(function(){
var newUrl = 'index.php/'+$(this).attr('href');
$(this).attr('href', newUrl)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
you link
Include above code into your views

I think you now have to change it mannually, but start using Codeigniter functions in URL Helper.
Take a look at base_url() and site_url() they are very usefull when you need to be prepared for changes like yours.
For example, in your code you can put:
<li class="active">Home</li>
or
<li class="active">Home</li>

In config set your base_url to this
$config['base_url'] = "your_site_url/index.php/";

Related

Codeigniter URL Overlapping. Not working properly

this is my code below.
<li>Why share?</li><li>
But after I apply this i manage to redirect to the page but when i try to press another page the url will be keep adding like this and cause page not found.
The page look like this
But when i try to link to home page, the url won't change back to /home only
Any solution? or another way to link the page?
Need help on this!
Thanks!
Your href should have a slash in front of it so that it goes to "root".
<li>Why share?</li><li>
If not, the browser will think it is relative to the current route. Or use Codeigniter's built-in site_url() function
<li>Why share?</li>
Read about relative/absolute here: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/
You need to set your base_url() in config.php, and call the url_helper so that you can then use it.
Step by Step Instructions:
in application/config/config.php, set your base_url, I prefer to use something like this:
$config['base_url'] = 'http://' . $_SERVER['SERVER_NAME'] . '/';
in application/config/autoload.php, add url helper:
$autoload['helper'] = array('url');
Use it in your views like this:
<a href="<?= base_url('about/why') ?>" > link </a>
Read this: https://www.codeigniter.com/user_guide/helpers/url_helper.html

Managing Links within PHP Includes using htaccess

Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.

CSS Menu URL's not directing appropriately

I created a CSS menu, to use on a PHP site, but now I realised that for example:
From the Home tab (URL localhost/site), all the item links are correct if you hover over them, but now i navigate to "Stock" for example, who's URL is localhost/site/stock... it opens correctly. So from Stock i want to navigate to Sales (localhost/site/sales), the URL of sales become localhost/site/stock/sales and not just localhost/site/sales.
I added this menu only recently, the previous one was working fine, so for a test, i replaced the css menu with the previous one, but to no avail. the problem still persist, so I assume that something else must have changed the behaviour of the links...
I can probably fix this by added " ../ " in front of the menu's URL, but on the other hand. not all items are just one step back.
Your help would be greatly appreciated.
Try using absolute URLs.
When your link is on a page located at localhost/site/stock:
Link <!-- goes to localhost/site/stock/sales -->
But if you prepend a forward slash, the destination will be absolute, relative to your document root:
Link <!-- goes to localhost/site/sales -->
Assuming of course that your document root is localhost/site
More likely your links will have to look something like:
Link
You're correct in saying you need to add slashes. My suggestion would be to provide absolute links from / to your page:
/site
/site/sales
/site/sales/sale24
/site/stock
/site/stock/secondstockpage
/site/stock/thirdpage
I have changed the menu as you suggested, but now, it works as follow:
if i for example set my menu link to direct to: /site/sales/sold.php
so the menu.php file link would look like this:
<li>Sales</li>
<ul>
<li>Sold Items</li>
</ul>
it would sometime direct to /site/sales/sold.php and the next moment, it repeats the menu's directory like: /site/sales/sales/sold.php
so i removed the sales directory, as its directing there by itself, worked for a little while and now it directs to /site/sold.php which does not exist so it ends up with a 404.
should i specify a siteroute somewhere else?
this is very weird to me. first time i encounter something like this!
Hope my examples make sense!

Codeigniter URL index.php twice

Hi evrybody ‘im new to codeigniter and to MVC model as well,
I need help to get my project working couse i’ve got this issues:
1) i’m in the main page and i clik on the link of the “about” page, the css file does not seems to load.
and even if i include the css in the head section the images are still missing in the page.
2) in the configure file i’ve set the
$config[‘base_url’] = ‘’;
$config[‘index_page’] = ‘index.php’;
now if i’m in the about page and i clik again on the about link the link it’s missing becouse i set the link in the menu:
About
and so the index.php it’s loaded twice: localhost/mywebsite/index.php/index.php/about.
i could set the condition to cut the link if i’m in the page i need but i would like to know if there is a more polite solution
and hope that the solution it’s not to put my hands in to the mod_rewrite .
I’ll like codeigniter because seems to be easy to configure and so really portable.
and even if it’s simple i can’t figure out how to solve this issues
Thank you everybody for your time!
Even i a newbie to CodeIgniter. I was facing the css problem. I added the css files in separate folder located at the root directory. I solved the problem by adding $this->load->helper('url'); in the controller function of the page.
You need to set $config['base_url'] in config.php to http://www.yourdomain.com and provide full path when linking css to the page like this :
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>resources/css/file.css">
just add
> <?php echo base_url();?>
before or or URL link
> (example: <script src="<?php echo
> base_url();?>assets/js/jquery-1.7.1.min.js"></script>)
to load the link to all page and separate the header and footer in separate file which could help you a lot minimize the page content.

Same page link not working in Codeigniter

I have written my own blog let’s say at example.com.
I’ m trying to use footnotes for my posts.
So I have a post at the address:
http://www.example.com/blog/2012/04/post-slug
I use this code for the footnotes (produced by markdown-extra):
<p>That's some text with a footnote.<sup id="fnref:1">1</sup></p>
<div class="footnotes">
<hr />
<ol>
<li id="fn:1">
<p>And that's the footnote. ↩</p>
</li>
</ol>
</div>
This code is saved in the database as the post’s body.
The problem is that when I point to the browser to the post and click the links they point to the home page of my site like this:
http://www.example.com/#fnref:1
and
http://www.example.com/#fn:1
Instead of the correct:
http://www.example.com/blog/2012/04/post-slug#fnref:1
And
http://www.example.com/blog/2012/04/post-slug#fn:1
respectively.
That is they don’t take in mind the part of the URL
/blog/2012/04/post-slug
They "think" they are in the home page for some reason.
In Codeigniter I have chosen not to include index.php in the URL. And of course I have some routing definition in the routes.php file.
I cant’ figure out why this problem occurs. Maybe it has to do with routing.
I have tried all the available options in the config.php file:
$config['uri_protocol'] = 'AUTO';
(PATH_INFO etc) but didn’t help.
Can anyone help?
Thanks in advance.
CI always behaves as it's in the root this because that's where its index.php file is located and all the controllers/methods are called from there.
To solve your case, I'd suggest you add a path to your href attribute, or use base, like Zenbait said.

Categories