Hi People

The Problem:
>
I want to change permently so you never see the folder named "2".
I thought this would be possible through the htaccess file or web hosting server?
So using mod_rewrite or changing the host directory of your server to www/2/ instead of www/?

Thanks, Regards X

PS: Question: modrewrite, etc only maniuplates the URL it does not actually change values of .php, variables and root directories?

Dani AI

Generated

asked how to hide the "2" folder so www.1.com/2/3.php appears as www.1.com/3.php. was right that changing the server DocumentRoot to the www/2 folder will do this at the server level, but that requires control of httpd.conf (not usually possible on shared hosting). A safer, portable method is to use Apache mod_rewrite in the document-root .htaccess to both canonicalize old /2/... links and internally serve files from /2 while keeping clean URLs.

Try this approach (place in the site root .htaccess and ensure mod_rewrite and AllowOverride are enabled):

RewriteEngine On

# Redirect direct client requests for /2/... to /... (301, canonicalize)
RewriteCond %{THE_REQUEST} \s/2/([^\s?]*) [NC]
RewriteRule ^2/(.*)$ /$1 [R=301,L]

# If the requested file/dir does not exist in root, internally serve from /2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 2/$1 [L]

Explanation: the first rule issues a 301 redirect for any client-requested /2/... URL so search engines and bookmarks move to the new canonical path. The second rule silently rewrites clean URLs to files inside /2 only when the requested resource does not exist in the document root, avoiding loops (using %{THE_REQUEST} prevents matching internal subrequests). Note that internal rewrites do not change the browser address bar; the server runs the file from /2, so PHP globals like $_SERVER['SCRIPT_FILENAME']/__FILE__ will point to the real file while $_SERVER['REQUEST_URI'] still shows the requested (clean) URL. If your PHP uses relative includes, switch to absolute paths or adjust include logic. For authoritative behavior and flags see the Apache mod_rewrite documentation: Apache mod_rewrite Documentation.

Recommended Answers

All 2 Replies

you can also set the root directory of your server to www/2/ folder if your using Apache webserver edit the httpd.conf and change the root directory there. or you can also put your 3.php file in your www folder since that is your default root.

That was the first idea I had but im yet to attempt as I would like to gather a 'source' of ideas and see which is best.
I am assuming if changing the root is possible would be the best attempt as the 'server' can still access the files where as human counter parts cannot! So its a win win situation to me, only thing is if it is do able on my domain as there are multiple I have been informed :( Keep the ideas coming guys, much appreciated. Thanks.

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.