1

Site is currently moved from Production to Development server. So the previous setup would be http://production.com/ and now the development setup is http://development.com/site/

My problem is that almost all of the assets are pointing to absolute path, like the js, css and images.

Example:

JS: <script src="/assets/path/to/js/script.js"></script>

CSS: <link rel="stylesheet" type="text/css" href="/assets/path/to/css/style.css" />

IMAGE: <img src="/assets/path/to/images/image.jpg">

All of these assets will not load because it all points to http://development/assets/. It should be http://development/site/assets/

Is there a way to fix this using htaccess? I don't want to go around site and change all paths to relative since it would be a big job. I just want to use something simple so that when moving back to production where it is not in subfolder anymore, it would be easier to migrate by just changing the htaccess file.

P.S. htaccess should be within the subfolder /site/ because i don't want placing it the development root since it would affect other subfolders within the development root.

Thank you.

Tinker
  • 299
  • 2
  • 4
  • 16
  • The `/assets/path/to/js/script.js` URL is [**relative**](https://www.w3.org/TR/WD-html40-970917/htmlweb.html#h-5.1.2), not absolute. – Alexander Mar 25 '18 at 06:21
  • It's kinda fall into absolute path, as it loads `http://development.com/assets/path/to/js/script.js` and not relative to current URL w/c should be `http://development/site/assets/path/to/js/script.js` @Alexander if you have idea how to fix this, please it would be a great help. Thanks – Tinker Mar 25 '18 at 06:32
  • Now I see, you want to "insert" *site* segment to URLs, don't you? – Alexander Mar 25 '18 at 06:41
  • You better have a config file with base_url variable that you can use at every page to combine with your relative urls so that next time you move to another location, all urls will work automatically without problems. When you move to another location, you just update that variable. – Karlo Kokkak Mar 25 '18 at 06:49
  • @Alexander Yes, but i don't want to do it by updating all the resources path within the website. If using htaccess will do it, that would be great. Thanks – Tinker Mar 25 '18 at 06:49
  • 1
    Do you use and `` tag in your sites? – Lithilion Mar 25 '18 at 07:06
  • @Lithilion `` tag only works if the source path is `assets/path/to/images/` but the source paths are `/assets/path/to/images` w/c becomes an absolute path and ignored the `` tag. – Tinker Mar 25 '18 at 15:47

1 Answers1

0

You could use one of the following:

  1. Create rewrite rule within  .htaccess file:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/site/
    RewriteRule ^/(.*)$ /site/$1 [QSA,L]
    
  2. Use the <base> HTML tag that set the root application URL as base URL of the page.

Alexander
  • 4,420
  • 7
  • 27
  • 42
  • 1
    Both doesn't work. 1. .htaccess rule you wrote above seem not to work. 2. Doesn't work too since the source paths starts with forwardslash i.e. `/assets/path/to/images/` . This only works if the path doesn't start with forwardslash such as `assets/path/to/images/` – Tinker Mar 25 '18 at 15:51