Solved

.htaccess - change document root

Posted on 2008-10-17
7
10,664 Views
Last Modified: 2013-12-13
Hello,

I have the following directory structure:

<ROOT>
    - .htaccess
    * mydomain.com
        - index.php
        * images

What I need is that every single hit to http://www.mydomain.com/anything would be transparently translated to http://www.mydomain.com/mydomain.com/anything. Thus the address in browser would not change in any way ... Need just hidden folder rewrite.

Please note that if the structure would look like this:

<ROOT>
    - .htaccess
        * mydomain.com
            - .index.php
            * mydomain.com

Then any hit to www.mydomain.com/mydomain.com/anything would be actually translated to www.mydomain.com/mydomain.com/mydomain.com/anything and without changing the browser's URL IN ANY WAY. Just transparent translation.

I tried this code (doesn't work...):

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /mydomain.com%{REQUEST_URI} [QSA,L]

or

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ mydomain.com/$1 [QSA,L]

or

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /mydomain.com/$1 [QSA,L]

Nothing worked well... do you know how to transparently redirect the document root?

I'm using Apache + PHP + mod_rewrite (I don't have access to httpd.conf).

Thank you very much.
0
Comment
Question by:JiriNovotny
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 22745074
> (doesn't work...):

That means it doesn't work at all, a HTTP 404 is being returned? You may test the functionality of mod_rewrite with a simple rule which redirects every request to google:
RewriteEngine on
RewriteRule ^  http://www.google.com  [R,L]

Open in new window

0
 

Author Comment

by:JiriNovotny
ID: 22745505
Well mod_rewrite works great, just the rewrite rules don't work as expected.

Eg:

When I enter www.mydomain.com/localdir it is redirected (in browser URL) to www.mydomain.com/mydomain.com/localdir. And it should NOT be redirected that page in browser's URL. Redirection should be transparent.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22746202
First things first -- the HTACCESS file is IN the web root folder, and is read from that ROOT folder.  So you cannot use the HTACCESS to redirect the ROOT folder, since it has to reside in the root folder.

What you are asking I think is to translate all pages in public_html of your root to -- public_html/folder2 of your website, is this correct?

Also you need to state the this is normal Linux hosting, as M$ IIS hosting is TOTALLY different.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:JiriNovotny
ID: 22747552
Yes, .htaccess is in root folder and it can be in root folder, just the actual website data must be in subfolder.

Yes, I need to translate all pages/images/data in public_html to public_html/folder2.

And yes, this is normal Linux hosting, sorry for confusion.
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 22747940
> it is redirected (in browser URL)

But not through your rules above, none should cause a redirect (no R-flax, no scheme+fqdn).

I assume that there are no other rules which may interact with the one below.
RewriteEngine On
# fix trailing slashes since mod_dir will redirect to /mydomain.com/...
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ %{REQUEST_URI}/ [R=301,L]
 
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^(.*)$ /mydomain.com/$1 [L]

Open in new window

0
 

Author Comment

by:JiriNovotny
ID: 22748305
caterham_www:
Thanks, your solution is almost perfect. Just one last question:

I think the redirection was caused by mod_dir. Is it possible (and how) to disable mod_dir in .htaccess?
0
 

Author Comment

by:JiriNovotny
ID: 22748463
OK, I've figured out and attached the solution. Thank you for helping me figuring this out.
Options +FollowSymLinks +MultiViews -Indexes
Options -Indexes
 
IndexIgnore */*
 
<IfModule mod_dir.c>
 
  DirectorySlash Off
 
</IfModule>
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
 
  # The mydomain.com domain redirect
  RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
  RewriteRule ^(.*)$ /mydomain.com/$1 [QSA,L]
 
</IfModule>

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question