timetraveller92 -8 Light Poster Banned

I've got a domain-name that points to one IP
When user types the domain name it should point to subdirectory's index.php
When user type ip address it should open the index.php in root

Anyone knows how to perform this? I've been using javascript top.location and even htaccess can't seem to make it work.Struggling to make it work, can you advise? X_X

Dani AI

Generated

— the behavior you want is determined by the HTTP Host header coming from the browser, not DNS. The clean, reliable fix is server-side: configure name-based virtual hosts (Apache) or server blocks (nginx) so the domain's vhost serves the subdirectory and the default/catch-all vhost serves the site you want when the IP is requested. Client-side redirects (javascript) are fragile and avoidable.

Example Apache approach (preferred if you control the server):

<VirtualHost *:80>
    # default / catch-all (put this first)
    DocumentRoot /var/www/html
    ServerName 123.123.123.123
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/subdir
</VirtualHost>

If you are on shared hosting and cannot create vhosts, use a per-directory rewrite that only affects requests with your domain and only the site root (so you do an internal rewrite, not an external redirect):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /subdir/index.php [L]

Troubleshooting: run apachectl -S (or nginx -T) to see which vhost is active, and test with curl to simulate Host headers: curl -v -H "Host: example.com" http://123.123.123.123. If assets break, consider using a document root pointing to the subdir, Alias/symlink, or adjust base href. See the Apache docs on virtual hosts and mod_rewrite for details: Apache name-based virtual hosts and mod_rewrite.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.