Home > Blockchain >  Using .htaccess to point absolute page links to directory instead of root domain
Using .htaccess to point absolute page links to directory instead of root domain

Time:07-24

My local development web server has all my different project folders, for example:

https://localhost/project1
https://localhost/project2

Due to dynamic content, some of my projects require absolute links, such as /images/example.jpg - when they are uploaded to my web server, under their appropriate domain they work perfectly, for example domain.com/images/example.jpg

However on my local server they do not, because they point to localhost/images/example.jpg (obviously), however I need the root directory to be viewed as ./project1 so I need the link to be interpreted as localhost/project1/images/example.jpg

My current solution is I have this in my root .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )$ /project1/$1 [L,QSA]

However this only lets me rewrite the links for one dynamic project folder at a time, which is problematic if I want to jump between projects.

How can I do this from .htaccess in the project folder so I can develop on my local server with ease without having to switch directories in the root .htaccess file?

Edit: with the help from arkascha I want to further clarify my question:

With .htaccess I would like to check if a file/directory is existent in the root directory (as seen in my example), if is not, I would like it to look for the file in the directory of the referrer (the first directory after root).

CodePudding user response:

Based on your comments to the question this probably is what you are looking for:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https://[^/] /([^/] )/
RewriteRule ^/?images/ /%1%{REQUEST_URI} [END]

You can implement such rules in the http server's host configuration, or, if you have no access to that, you can use a distributed configuration file (".htaccess"), if you have enabled the consideration of such for that http host ...

CodePudding user response:

Considering the clarification you made in your question after I offered my first answer, I would like to bring my first suggestions to notice again: using different host names. To me this sounds like the more promising approach to your situation.

You can use different hostnames locally without much effort. And if this only is about your local development environment anyway, then you also don't need to care about valid ssl certificates.

You just need to take care that a name resolution for such host name succeeds. All operating systems offer means for that, but the details obviously differ slightly. Unless you made really surprising changes to your local name resolution setup you should have a "hosts" file in your system where local host names can be registered in a static manner along with the address those names should get resolved to. That is effective, easy to setup and solves your issue.

Given that you can define a local host for each of your projects. Thus requesting these hosts just like your projects would get requested in production. This way your development setup stays much closer to the actual production setup you implement your projects for.

This would be an example for such a "hosts" file, here on taken from a typical Linux based system:

# Host addresses
127.0.0.1  localhost
127.0.1.1  bragi
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters

You can simply add entries there using any plain text editor, for example you could append:

127.0.0.1  project1
127.0.0.1  project2

All that is left is to setup separate virtual hosts for those projects inside your http server:

<VirtualHost *:80>
        ServerName project1

        LogLevel notice
        ErrorLog ${APACHE_LOG_DIR}/project1/error.log
        CustomLog ${APACHE_LOG_DIR}/project1/access.log combined

        DocumentRoot /var/www/hosts/project1
        <Directory /var/www/hosts/project1>
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        ServerName project2

        LogLevel notice
        ErrorLog ${APACHE_LOG_DIR}/project2/error.log
        CustomLog ${APACHE_LOG_DIR}/project2/access.log combined

        DocumentRoot /var/www/hosts/project2
        <Directory /var/www/hosts/project2>
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

That is all you need to request your local http server using different host names for different projects:

http://project1/foo
http://project2/bar

You could even use the https protocol locally using a snake oil certificate, you'd have to accept that certificate once for every project then in the browser used for testing.


The bottom line:

You use a host name per project, just as you do in production. This keeps your local development setup much closer to the actual production setup. You don't need any fancy rewriting rules.

I personally chose to include common, repeated configuration options for those virtual hosts in a shared configuration file I include in those host definitions above. But that is a question of personal choice, there are endless options, obviously. The documentation of the apache http server and all its glorious modules will answer all questions about that. That documentation is of excellent quality and comes with great examples, as typical for OpenSource projects.

  • Related