0

I recently had to move a CodeIgniter based website from the web root of:

/var/www/vhosts/dev/

to:

/var/www/vhosts/dev/site1/

The problem I am encountering is that I need the web root to still remain accessible from the domain: www.devdomain.com

All relative links now are broken since they obviously were referring to the web root. Is there a rewrite rule or something at the top-level I can use that will automatically add the site1/ to all relative based links?

So, what would have normally been;

<a href="/enrollment">Enrollment</a>
https://www.devdomain.com/enrollment

Now needs to become;

<a href="/enrollment">Enrollment</a>
https://www.devdomain.com/site1/enrollment

Any help would be extremely appreciated.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Skittles
  • 2,866
  • 9
  • 31
  • 37

2 Answers2

0

The $config['base_url'] fix Nikola mentions would work if you used, in your links, the

<a href="<?php echo base_url(); ?>/enrollment">Enrollment</a>

Since apparently you don't want to go through the code the problem isn't in Codeigniter but in the server (?). Maybe a RewriteRule in the .htaccess file can help you (if you're on Apache).

I can't comment other answers since my rep level is low, otherwise I would've simply commented on Nikola's answer.

EDIT: I just figured this out. You have to remove the initial "/" from the href attributes! It is going to the root of the webserver, it is being a relative link but not the way you think. If you remove the "/" the link will go to a file on the same directory level of your file (in this case the Codeigniter installation). Sorry but you'll have to go through your whole code.

Additionally I would recommend for you to use site_url() to link between controllers inside Codeigniter, helps with this kind of situations and a lot others.

SECOND EDIT: It's not site_url() but base_url(), sorry for the confusion. Just corrected the code.

jakobhans
  • 826
  • 7
  • 16
  • I tried adding this to my rewrite rule, but it didn't appear to work. RewriteRule ^(.*)$ site1/index.php?/$1 [L] And my RewriteBase is now /site1/ Is that the correct approach? – Skittles Jan 25 '13 at 17:37
  • I will try that and see how it works. That makes perfect sense! Let me go give that a shot and see what happens. – Skittles Jan 25 '13 at 17:55
  • That definitely seemed to resolve the issue with the links. When I tried to apply that same logic to the SRC paths for css, no styles loaded. Does that make sense? – Skittles Jan 25 '13 at 18:15
  • But CSS files are referenced by href, not src: You can also use the base_url() method in Codeigniter to prepend to the URL. – jakobhans Jan 25 '13 at 18:19
  • Thank you, jakobhans. I really appreciate your help with this. :) – Skittles Jan 25 '13 at 18:31
0

Please see my answer here on the topic of base_url() vs site_url() and why you use one over the other.

How to set proper codeigniter base url?

For generating links to CodeIgniter generated pages, site_url() is in fact the correct function. Additionally, passing the url as the argument to the function as a string makes generating urls cleaner. Observe:

<a href="<?php echo base_url(); ?>/enrollment">Enrollment</a>

vs:

<a href="<?php echo site_url('enrollment'); ?>">Enrollment</a>

or even:

<?php echo anchor('enrollment', 'Enrollment'); ?>
Community
  • 1
  • 1
Brendan
  • 4,565
  • 1
  • 24
  • 39