1

How do I remove a route segments from my base URL? For instance, I have these routes in my routes.php:

$route['default_controller'] = 'main_controller';
$route['users'] = 'user_controller';
$route['user/(:num)'] = 'main_controller/user/$1';

They all get routed normally, but in the view called by the user() method all of the generated links (for .js, .css, .png files, etc...) have user/ inserted betweem base URL localhost/domain_name and file path (e.g. css/style.css), so all of those links (as you would expect) return 404 and the page isn't formatted properly.

If you need more info on the code in question, just comment what you need and I'll add it. Thanks in advance.

EDIT: If it's any help, echo base_url(); in the view prints out http://localhost/domain_name/.

10
  • 1
    In view file, call absolute path (i.e. /css/style.css). Commented Dec 22, 2017 at 12:51
  • @Tpojka But now it doesn't prepend the base URL automatically? Commented Dec 22, 2017 at 13:06
  • 1
    You have to set scheme prepended and also forward slash at the end in base url (i.e. $config['base_url'] = 'http://localhost/domain_name/). Commented Dec 22, 2017 at 13:58
  • 1
    Have you set it and tried? You shouldn't experience issue with base_url correctly set. Commented Dec 22, 2017 at 14:31
  • 1
    This is one your comment: $config['base_url'] = 'localhost/domain_name'; which is not even close what I asked after seeing your comment. I am sharing my experience. If fully qualified URL path is not set, meaning scheme+host+path+trailing slash I was able to notice similar issues. Other thing you can check is browser cache: close all browser windows and try again but in private mode. That way you should be certain there ain't no caching pages. Commented Dec 22, 2017 at 14:50

1 Answer 1

3

add base_url before the script and css files. and specify the folder in which is kept.Like we have style.css file in assets/css folder

<?php echo base_url('assets/css/style.css'); ?>
Or 
<?php echo base_url().'assets/css/style.css'; ?>
Sign up to request clarification or add additional context in comments.

6 Comments

I tried that, it also adds the user/ in front of it.
can u specify what the base_url ?
$config['base_url'] = 'localhost/domain_name';
That's correct . where u use it. can u show the code ?
OK, this seemed to work, but I had to change the base url, as @Tpojka helpfully suggested. The problem was in the link I was testing it on (it was broken).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.