PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

moving project to a subfolder messes my script path and url

I am developing the website in my local server in a root folder, the problem is that when my client told me to upload the website in to their server, it is located in the subfolder. So the url has been like this. http://example.com/myproject

The problem with that format is that all my css,js, ajax calls are messed up because when I tried to check Firebug/chrome console, I am seeing a http://example.com/assets/css/main.css , where in fact it should be http://example.com/myproject/assets/css/main.css

All my scripts are coded to be like this: <link rel="stylesheet" href="/assets/css/main.css"> since that works perfectly when the project is not in a subfolder.

My question now is that, is there a trick, maybe in the .htaccess or mod_rewrite that would allow me to tell the browser that always add a /myproject in all my script calls?

The reason for this is that I don't want to change all my script calls and add a /myproject/...

This is the sample of my .htaccess:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    <Files .*>
        Order Deny,Allow
        Deny From All
    </Files>

    # Allow asset folders through
    RewriteRule ^(fuel/modules/(.+)?/assets/(.+)) - [L]

    # Protect application and system files from being viewed
    RewriteRule ^(fuel/install/.+|fuel/crons/.+|fuel/data_backup/.+|fuel/codeigniter/.+|fuel/modules/.+|fuel/application/.+) - [F,L]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule .* index.php/$0 [L]

    # Prevents access to dot files (.git, .htaccess) - security.
    RewriteCond %{SCRIPT_FILENAME} -d
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>
Options -Indexes

Your help will be greatly appreciated! Thanks!

Upvotes: 2

Views: 90

Answers (2)

MrWhite
MrWhite

Reputation: 45829

If you are unable/unwilling to edit your site then you will need to edit the .htaccess file (or server config) in the site's document root. To rewrite all requests for non-existent files to your project's subdirectory:

For example, something like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/myproject%{REQUEST_URI} -f
RewriteRule !^myproject/ /myproject/$0 [L]

For all requests for files not in the /myproject subdirectory, that don't exist, but do exist in the /myproject subdirectory then internally rewrite the request to the /myproject subdirectory.

This assumes that your (sub)site consists of real files that exist on the filesystem.

This will make it look as though your project is hosted in the document root. You could externally redirect (R=301) - but that would result in every page triggering multiple redirects which is to be avoided!

However, whether this works at all will depend on what else your client is hosting on their site.

I think the best solution is to edit your files.

UPDATE: Since your site has a front controller (ie. you are routing all requests for non-existent files through index.php) then the above directives won't quite work.

However, if the client is simply hosting a site that consists of real files on the filesystem then this could probably be simplified to:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^myproject/ /myproject/$0 [L]

Upvotes: 1

kinadian
kinadian

Reputation: 238

In Unix based filesystems (which the web path is based on), the / at the beginning of a path is an absolute path to the root level.

For web addresses, this means it goes directly to the document root of your website.

You will need to remove the beginning / from your href attributes

<link rel="stylesheet" href="assets/css/main.css">

Upvotes: 0

Related Questions