Search code examples
phphtmlapacheurl-rewritingubuntu-24.04

How to fix php/html code after moving service to subfolder (Apache)


I'm using 2 virtual hosts for separate services (having separate document roots) on Ubuntu 24.04 server accesed by:

http://server1.domain.com and http://server2.domain.com,

but I'm forced to move both of them to one virtual host, they will be in different subfolders sharing same document root. They will be accesed by: http://server.domain.com/service1 and http://server.domain.com/service2.

Obviously all links to files are missing subfolder name in the code. I'm wondering how can I fix that without changing all the code.

situation looks following:

/var/www - root folder
/var/www/service1
/var/www/service2

I've buit conf file in following way:

<VirtualHost *:80>
        ServerName server.domanin.com
        ServerAlias *.domain.com
        <Directory /var/www/service1>
      <Directory /var/www/service2>
              DirectoryIndex service1_login_page.php
      </Directory>
      <Directory /var/www/service2>
              DirectoryIndex service2_login_page.php
      </Directory>
</VirtualHost>

I was trying adding rewrite rules inside like: RewriteRule #RewriteRule ^([^/]*)$ /service1/$1 [NC]

but I wasn't successful... How should I build my conf file ?


Solution

  • Use trailing slashes on the URLs:

    Then by the rules of URL pathname resolution, the default base of any those hypertext documents is the specific subdirectory.

    See Apaches' mod_dir module for configuration directives.

    Compare with 2.3 Hierarchical schemes and relative links in RFC 1738 etc..

    All the links do not need the subfolder name in code after all.

    And in case you have prefixed them with a leading slash, put a dot (".") in front of the leading slash or remove the leading slash completely.

    This is often a simple search and replace operation with standard text utilities. Exclude the .git folder though.

    And to ease the code-review diff only within words, not lines.