Asked  7 Months ago    Answers:  5   Viewed   26 times

How do I set a base URL for my website and get it to include in every page?

Is there a way for me to easily change a variable to be the base url for the website, such as <?php $baseurl = ""; ?>, and include this on every page so that all CSS, JavaScript, images and PHP includes follow this $baseurl?



You can’t make both PHP and client-side assets use the same base URL, unless you use PHP to echo a base URL variable or constant to the page.

The usual approach is to have a bootstrap file that you include on every page, and define your base URL and other site-wide variables in there.


    define('BASE_URL', '');


<!DOCTYPE html>
    <!-- // -->
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
    <!-- // -->
Wednesday, March 31, 2021
answered 7 Months ago

Try this:

$str = ' file [that] needs "to" be (encoded).zip';
$pos = strrpos($str, '/') + 1;
$result = substr($str, 0, $pos) . urlencode(substr($str, $pos));

You're looking for the last occurrence of the slash sign. The part before it is ok so just copy that. And urlencode the rest.

Wednesday, March 31, 2021
answered 7 Months ago

According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.

Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.

$url = urlencode($url);
Friday, May 28, 2021
answered 5 Months ago

asset_host doesn't work for urls

You need to override default_url_options in your ApplicationController (at least in Rails 3)

class ApplicationController < ActionController::Base
  def default_url_options
    if Rails.env.production?
      {:host => ""}
Thursday, June 3, 2021
answered 5 Months ago

You need to set the property server.contextPath to /myWebApp.

Check out this part of the documentation

The easiest way to set that property would be in the properties file you are using (most likely but Spring Boot provides a whole lot of different way to set properties. Check out this part of the documentation


As has been mentioned by @AbdullahKhan, as of Spring Boot 2.x the property has been deprecated and should be replaced with server.servlet.contextPath as has been correctly mentioned in this answer.

Thursday, June 17, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :