Solved

.htaccess - change document root

Posted on 2008-10-17
7
10,658 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

773 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