Home > OS >  I'd like my "/" links to point to a specific subfolder when clicked from my public UR
I'd like my "/" links to point to a specific subfolder when clicked from my public UR

Time:01-24

I'm working on a project with some friends that needs a little website I'm developping.

It's host on my computer using Wamp server, and I configured it this way :

  • C:\wamp64\www\projectfolder\ is the folder where my project is.
  • http://example.online/ is the virtual host pointing to my projectfolder that I use to access my website locally.
  • http://example.ddnsservice.org/projectfolder/ is the URL to access my website whenever I need to share it.
  • http://example.ddnsservice.org/ i.e. points to the default wampserver page.

Working this way, all the relative links pointing to / or /something/else/ are working when I use my virtual host URL but don't when I use the public URL. /models/ would redirect to http://example.ddnsservice.org/models/ instead of http://example.ddnsservice.org/projectfolder/models/.

I've tried to use RewriteRule on the .htaccess file in projectfolder following several solutions proposed here but nothing seemed to work so I begin to think that I have a deep misconception of what hosting a website should look like. The last thing I tried was:

RewriteEngine on
RewriteCond "%{HTTP_HOST}" "^(.*)example.ddnsservice.org(.*)$"
RewriteRule ^/$ ^/projectfolder/$

I didn't try to make http://example.ddnsservice.org/ point to the projectfolder because I sometimes share others projects and because I don't know how to do it.

I'm pretty lost atm and don't really know from where to take this problem anymore.

Thanks everyone for reading me and giving a bit of your time.

CodePudding user response:

Don't use the / as prefix in your links. Let's say your server has a folderstructure like this:

htdocs (public-html)
  |- projectfolder
       |- images
            - logo.png 
            - photo.jpeg
       - index.html

So you want to link from the index to a different file. In your question a model for example, or in the situation above to an image. In this case you need to link to "images/logo.png". As your index is located in the projectfolder already it will search from this location to a folder images and then enters the folder and looks for the logo.png. So the url would translate to http://example.ddnsservice.org/projectfolder/images/logo.png

If you would use "/images/logo.png" you're telling that it needs to move up one folder and start to search from there. As your index is located in the projectfolder, it will move folder up, to the htdocs (or public-html or something) and search from this location to a folder images and then tries to enters the folder and looks for the logo.png. So that url would translate to http://example.ddnsservice.org/images/logo.png And that folder is the wrong folder, in this case it doesn't even existed.

CodePudding user response:

The answer from @t-s should solve your main problem. It's a matter of relative referencing. I am now editing my answer to help with some of the other parts of your post since I now have better understanding of what you don't understand.



I didn't try to make http://example.ddnsservice.org/ point to the projectfolder because I sometimes share others projects and because I don't know how to do it.

If you wanted to do this, you would need to change Apache default settings, like in this answer: https://stackoverflow.com/a/5891858/10335389

It will be a platform specific thing, but you basically need to locate the httpd.conf file, which might be under wamp/apache2/conf/ for you since you use Wamp. In that file, find the DocumentRoot setting and append "/projectfolder" to the value. You will probably find it as c:\wamp\htdocs, and you change it to c:\wamp\htdocs\projectfolder. Make the same change to the <Directory "c:\wamp\htdocs"> setting (which should be on the next line).



I sometimes share others projects

For this, you can set up multiple virtual hosts. I have not used this myself so I wont say much about it. I am only sharing this in case you are interested in making your projectfolder the root, and later want to share a different project (also as root).

In case you don't need the sites to be running simultaneously, then you can use symbolic links for (since I use Linux, they're easy to do), but on Windows, you might want to either use aliases to make the "projectfolder" point to a different folder each time, OR, only keep a copy of the project you want to serve in "projectfolder". In my opinion, the latter is convenient.



I've tried to use RewriteRule on the .htaccess file in projectfolder following several solutions proposed here but nothing seemed to work so I begin to think that I have a deep misconception of what hosting a website should look like.

This would not a good way to go about this even if it did concern your problem.

According to the Apache docs (under the "When (not) to use .htaccess files" section):

you should only use .htaccess files when you don't have access to the main server configuration file.

It further goes on to say:

in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a <Directory> section in your main server configuration file.


I hope this helps, even though the problem was just a matter of relative referencing.

  •  Tags:  
  • Related