Problem with subdomain

Hello everyone!!!

I’m here once again to try to understand something.

I’m having problems creating a subdomain.

Enforce subdomain ownership was disabled, as I saw in a post here in the community.

I created the domain domain.com and added the subdomain app.domain.com and activated the Custom Document Root pointing to the “app” directory that exists in /home/user/web/domain.com/public_html/app

There is no .htaccess in this directory, but I still get this error

[Sat Feb 01 20:50:34.414505 2025] [core:alert] [pid 88500:tid 88544] [client xxx.xxx.xxx.xxx:0] /home/user/web/domain.com/public_html/.htaccess: <IfModule not allowed here, referer: https://app.domain.com/

How do I solve this?

Can you provide a detailed description? What problem have you encountered? Why should it be in the public_ html/app directory instead of the public_ html directory? The public_tml directory is the root directory of the website.

Hello yonli!

Root domain: domain.com
Root directory: public_html

Subdomain: app.domain.com
Subdirectory: public_html/app

I wasn’t very clear when I said that the directory doesn’t have .htaccess. In fact, it only exists in the public root directory. Inside the app directory, there is no .htaccess

Structure:

public_html/
          .htaccess
          index.php
          page1.php
          page2.php
          page3.php

public_html/app/
          index.php

.htaccess:

# <IfModule mod_rewrite.c>

     RewriteEngine On

     #RewriteBase /
     RewriteCond %{REQUEST_FILENAME} -s [OR]
     RewriteCond %{REQUEST_FILENAME} -l [OR]
     RewriteCond %{REQUEST_FILENAME} -d
     RewriteCond %{REQUEST_FILENAME}\.php -f
	 
     RewriteRule ^home(.*)$ index.php?pg=$1 [L,QSA]
     RewriteRule ^page1(.*)$ page1.php?pg=$1 [L,QSA]
     RewriteRule ^page2(.*)$ page2.php?pg=$1 [L,QSA]
     RewriteRule ^page3(.*)$ page3.php?pg=$1 [L,QSA]
   
# </IfModule>

Error:

[Sat Feb 01 20:50:34.414505 2025] [core:alert] [pid 88500:tid 88544] [client xxx.xxx.xxx.xxx:0] /home/user/web/domain.com/public_html/.htaccess: <IfModule not allowed here, referer: https://app.domain.com/

If I delete the .htaccess, pointing to the app directory works perfectly, but I need the .htaccess with the friendly URL rules inside the public_html root directory.

The error message you are receiving indicates that the <IfModule> directive is not allowed in the context where it is currently placed in your .htaccess file. This is usually because the Apache configuration does not allow such directives within .htaccess files, or the server configuration might be limiting the use of <IfModule> to specific sections.

Here are a few steps to troubleshoot and potentially solve your issue:

  1. Remove <IfModule> Tags:
    Since you are encountering issues with <IfModule>, try removing these tags from your .htaccess file. This assumes that mod_rewrite is indeed enabled on your server, which is typically required for the rewrite rules to work.

Update your .htaccess to look like this:

RewriteEngine On

#RewriteCond %{REQUEST_FILENAME} -s [OR]
#RewriteCond %{REQUEST_FILENAME} -l [OR]
#RewriteCond %{REQUEST_FILENAME} -d
#RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^home(.*)$ index.php?pg=$1 [L,QSA]
RewriteRule ^page1(.*)$ page1.php?pg=$1 [L,QSA]
RewriteRule ^page2(.*)$ page2.php?pg=$1 [L,QSA]
RewriteRule ^page3(.*)$ page3.php?pg=$1 [L,QSA]

Note: The commented out RewriteCond lines were checking if the requested file exists and is a regular file, a symbolic link, or a directory. If you uncomment these, they ensure that the rewrite rules only apply if the file does not exist (this is a common practice to avoid rewrite loops). However, since you mentioned removing .htaccess from the app directory solved issues, it might not be necessary in your specific use case.

  1. Ensure mod_rewrite is Enabled:
    Contact your hosting provider or check your server’s Apache configuration to ensure that mod_rewrite is enabled. This is crucial for rewrite rules to work.

  2. VirtualHost Configuration:
    If you have access to the Apache VirtualHost configuration, you can place the rewrite rules there instead of in .htaccess. This is generally more efficient and avoids some limitations of .htaccess files.

Example VirtualHost configuration snippet:

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias app.domain.com
    DocumentRoot /home/user/web/domain.com/public_html

    <Directory /home/user/web/domain.com/public_html>
        AllowOverride All
        Require all granted
        RewriteEngine On

        RewriteRule ^home(.*)$ index.php?pg=$1 [L,QSA]
        RewriteRule ^page1(.*)$ page1.php?pg=$1 [L,QSA]
        RewriteRule ^page2(.*)$ page2.php?pg=$1 [L,QSA]
        RewriteRule ^page3(.*)$ page3.php?pg=$1 [L,QSA]
    </Directory>
</VirtualHost>

Note: Adjust the paths and configuration as necessary.

  1. Test Configuration:
    After making any changes, restart your Apache server to apply the new configuration. Test your site to ensure that the rewrite rules are working as expected.

By following these steps, you should be able to resolve the issue with your .htaccess file and get your rewrite rules working correctly for both the root domain and the subdomain.

Hello yonli!
I think you are not understanding correctly because you are using a translator to write in English, as my language is Brazilian Portuguese.

The reason for the subdomain is that:

  • Subdomain app.domain.com shows the page of the directory /public_html/app/
  • Main domain domain.com shows the page of the root directory /public_html/

I removed the <IfModule> tag and commented out RewriteRule inside .htaccess leaving it as in your example, I received the following error and it gave me the impression that for the pointing to work I cannot use url writing rules.

root@server:~# sudo a2enmod rewrite
Module rewrite already enabled
[Sun Feb 02 19:43:35.401266 2025] [core:alert] [pid 332851:tid 332865] [client xxx.xxx.xxx.xxx:0] /home/user/web/domain.com/public_html/.htaccess: RewriteRule not allowed here, referer: https://app.domain.com/

If I make the change in virtualhost as suggested by you, both the main domain and subdomain would point to the same public_html directory, which is not what I need.

If I remove the .htaccess from public_html and put the writing rules in virtualhost, the url writing does not work.


Consegui fazer funcionar como preciso criando registro CNAME no DNS ao invez de criar subdominio e adicionando essa regra no .htaccess

RewriteCond %{HTTP_HOST} ^app\.domain\.com$
RewriteCond %{REQUEST_URI} !^app/
RewriteRule ^(.*)$ /app/$1 [L,QSA]

The rule above worked, but I had problems with the subdomain SSL certificate

Using the Hestia panel on the web for domain certificate issuance

Hello yunli

Sorry for the delay in responding.

Man, thank you very much, you helped me a lot with your tips.

Eternally grateful.

Thank you!