Hi People

I have a url, http://www.domain.com.au/domain1/domain2.php .
Now I would like to remove the middle man aka folder from ever being seen so the url changes to, .
I have done some research and mod_rewrite looks to be the go in the htaccess file. So can anyone give me some examples or point me in the right direction?

Thankyou, Regards X

Dani AI

Generated

A focused, practical summary for the .htaccess approach that hides a subfolder (answering and building on ’s suggestions).

To keep the browser address as /domain2.php while actually serving /domain1/domain2.php, use an internal rewrite (no external redirect flag). The rule below rewrites requests that do not already contain /domain1/ and only if the corresponding file exists in that subfolder. It avoids loops and leaves any real root files alone.

RewriteEngine On
RewriteBase /

# skip requests that already point into /domain1/
RewriteCond %{REQUEST_URI} !^/domain1/

# only rewrite when the file exists in /domain1
RewriteCond %{DOCUMENT_ROOT}/domain1%{REQUEST_URI} -f
RewriteRule ^(.+)$ /domain1/$1 [L]

Notes on how it works and common pitfalls:

  • The first condition prevents infinite rewrite loops by ignoring requests that already start with /domain1/.
  • The second condition checks the filesystem for the target file inside domain1 so root files are not overridden.
  • ^ and $ are anchors; (...) creates a capture group referenced as $1 in the substitution. A literal dot in an extension must be escaped (for example, \.php) if matching only PHP files.
  • To test behavior visibly, temporarily add [R=302,L] to the rule to force a browser redirect; once correct, remove the R to keep the URL unchanged.
  • If an Internal Server Error appears, check the Apache error log. Common causes: mod_rewrite not enabled (remember ’s test server issue), invalid Options directives in .htaccess, disallowed AllowOverride settings, wrong file permissions, or a BOM at the start of the file.
  • If the site is driven by a front controller or CMS, similar logic applies but rules must be merged carefully to avoid breaking routing.

This pattern is a safe, general starting point. Adjust the matching pattern if only .php files should be rewritten or if deeper path handling is required.

Recommended Answers

All 7 Replies

general information and

howto, by the authors

Thanks but I have already gone through the apache manuals but it was very overwhelming at first and still could not reference what I required. I was hoping for a simple example inparticular the modrewrite for the folder but even something similar so I can gather a better understanding of the modrewrite function and then utilize it to my own requirements.

try

its my host's site
its pretty basic, by numbers,

commented: Nice Link! Thanks! +3

My htaccess file was spewing out errors when I used the code.
Any ideas? possible php.ini declerations? Thanks.

Options +FollowSymlinks
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

Note: The Error is "Internal Server Error"

is mod_rewrite available on the server?

Yes it is I forgot to turn it on my test server :D Ha Ha

The closest I have come across from your examples was:

Example #5:
The following will rewrite the URL to http://yourdomain.com/directory/newpage.html when visitors browse to http://yourdomain.com/anypage.html
RewriteEngine on
RewriteRule ^([a-z]+).html$ /directory/newpage.html [R,NC,L]

I am still not understanding the logic between conditional statements and output. ^$ start and end the string, [a-z] alpha characters, + multiple characters, .html the extension, then what is directory/newpage.html signify?

im looking something close to that but not a specific page just general 'hidden' folder for every page.

Off to reread the apache module :(

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.