Solved

.htaccess - change document root

Posted on 2008-10-17
7
10,670 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

739 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