?
Solved

.htaccess - change document root

Posted on 2008-10-17
7
Medium Priority
?
10,697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 1500 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 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