Hi,

I'm in need of assistance with mod_rewrite. Currently, I've a folder at www.domain.com/folder. I need to leave the files where they are, but have the address bar in the browser appear as someothername.domain.com

Any help would be greatly appreciated!

I should mention that I've already got a few .htaccess rules in place, as shown here:

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ index.php?$1 [QSA,L]

Thanks in advance,
-Ray

Dani AI

Generated

Short answer: mod_rewrite cannot secretly change the host shown in the browser — the hostname in the address bar either must be requested by the client (DNS + vhost) or the server must issue a redirect so the browser re-requests the new host. is correct that a rewrite that sends a 301 will change the browser URL. To keep the files physically in .../folder while showing someothername.domain.com without a visible redirect, the server must be configured to serve that hostname (virtual host) or act as a reverse proxy for it.

Two practical approaches (both avoid moving files):

  1. Point the subdomain in DNS to the server and add an Apache VirtualHost whose DocumentRoot is the existing folder. That makes someothername.domain.com serve the files in /path/to/www/domain.com/folder while keeping the address bar as the subdomain:
<VirtualHost *:80>
    ServerName someothername.domain.com
    DocumentRoot /var/www/domain.com/folder
    <Directory "/var/www/domain.com/folder">
        Require all granted
        AllowOverride All
    </Directory>
</VirtualHost>
  1. If creating a separate vhost with that DocumentRoot is not desirable, create a vhost for the subdomain that reverse-proxies requests to the existing folder. This keeps the subdomain in the address bar while fetching content from /folder:
<VirtualHost *:80>
    ServerName someothername.domain.com

    ProxyRequests Off
    ProxyPreserveHost Off
    ProxyPass        / http://127.0.0.1/folder/
    ProxyPassReverse / http://127.0.0.1/folder/

    # If the backend sets cookies or Location headers for "www.domain.com" or "/folder",
    # adjust with ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath as needed.
</VirtualHost>

Important gotchas and checks:

  • DNS: the subdomain must resolve to the server IP. Without DNS/vhost the browser cannot show that host except after a redirect.
  • HTTPS: a valid certificate for the subdomain is required (SNI/Let's Encrypt or a wildcard cert).
  • Cookies, absolute URLs and redirects from the app may still point at www.domain.com; use app config, ProxyPassReverseCookieDomain/Path or HTML rewriting to fix links and cookie domains.
  • Existing .htaccess front-controller rules (the index.php rewrites) may need RewriteBase or small adjustments after changing DocumentRoot or proxying.
  • Required Apache modules: enable mod_proxy and mod_proxy_http for proxying; mod_rewrite remains useful for path handling inside the folder.

In short: use DNS + a proper vhost (simplest and cleanest) or a reverse proxy when a vhost change is not possible. 's current .htaccess rules will still be relevant inside the folder but may require minor path adjustments once the subdomain is served.

The problem is more complex than a mod_rewrite recipe.

Rewriting the URL is fairly easily done:

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]  
RewriteRule .?  [R=301,L]

So when the browser requests a URL at www.domain.com, the server responds with a permanent redirect (a 301 response) that in effect says, “Oh, for that, you need to go to someother.domain.com instead, using the same URI.”

The browser happily obliges, requesting a URL at someother.domain.com.

The real question is: what’s at someother.domain.com? Is that domain set up? Is the a webserver set up to respond at that address? If not, that’s a different issue—a subdomain will have to be set up, and a webserver set up to respond to web requests for that domain.

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.