Htaccess challenge for you: redirect root domain to sub-directory

Here is the challenge, I invite you to help me solve it.

The .htaccess file needs to redirect from the root domain to /blog

Example (in english): www.mydomain.com redirects to www.mydomain.com/blog

The reason why is that I have no content at the root domain and would like to simply redirect users to the blog.

Seems rather simple, yet so far I’ve had no luck. And any .htaccess example I can find never attempts to achieve this setup. Generally it’s the opposite: from a subdirectory to the root directory, but never from the root directory to a subdirectory.

I’ll take a shot :wink:

RewriteRule (.*) http://www.example.com/blog$1 [R=301, L]

This will redirect all, or if you just want the home page, I’m thinking

RewriteRule .? http://www.example.com/blog [R=301, L]

Do NOT do this with .htaccess!

Instead, redirect your domain’s DocumentRoot to the blog directory. Do it the correct way, if you please!

The other way, since DocumentRoot is empty except for the blog subdirectory, move its contents to the DocumentRoot (and update the CMS’s config file).

Regards,

DK

Regards,

DK

oh ok, haha, thought I had it. sorry.

Take care.

Okay sure thing, changing the DocumentRoot setting seems logical, thanks.

CWg,

The “trick” to this question was that mod_rewrite is not the proper way to solve the problem.

As for your code, it’s good that you made the attempt (appreciated by all, I’m sure) but it would result in a loop (because of the inappropriate use of the :kaioken: EVERYTHING :kaioken: atom)! Whenever you use that, you MUST provide an escape from the loop. In this case, a RewriteCond testing the {REQUEST_URI} against ^blog/ would have been in order.

I’m NOT sure what your second RewriteRule was supposed to do except endlessly prepend blog/ to … er … blog/? The same RewriteCond could be used here, too.

As a “teacher” in this forum, I MUST correct code so that you (and other members who look at your code) understand the problem and know where/how to fix the loops! I hope this helped your understanding.

Regards,

DK

cool, thanks dklynn

Hy all!

I was searching the Internet for such a question!
The trouble (in my case) is that I have no access to vhost to use DocumentRoot, so htaccess is (I think) my answer. Anyway, in order to solve this problem with htaccess I’m trying the followin code (without success!).

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/blog [NC,S=1]
RewriteRule ^.*$ /pagenotfound.html [L]

RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteRule ^.*$ /blog/$1

So… what I am doing wrong?

Kromit,

First, WELCOME to SitePoint’s Apache Forum.

Second, this was an old thread that had been resolved (apparently). You should have started a new thread.

Third, you don’t need to have access to the httpd(-vhosts).conf file if you have access to cPanel (or another control panel) which can interface with the server for you.

As for your code :eek: where do I start?

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/blog [NC,S=1]
# Skip the next RewriteRule? :nono:
RewriteRule ^.*$ /pagenotfound.html [L]
# redirect EVERYTHING (which is not requested of /blog)? :nono:

RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteRule ^.*$ /blog/$1
# Finally, that makes sense - except for the lack of Last flag.

Perhaps a review of my signature’s mod_rewrite tutorial?

Regards,

DK

dklynn,

Thanks for answering (I assumed keeping things organized would be important, but I get your message on “old threads”…), and answering fast!

I’ll be checking my cPanel more carefully.

As for the code… that bad an? lol
I think I haven’t quite got the principles of the thing… Not that I haven’t done some working lines, but it seems so random…
I’ll be taking a look at your tutorial (you mean datakoncepts.com/seo, right?(no links for me, yet))

And posting some new threats with doubts, surely! :slight_smile:

Thanks again!

Kromitvs,

(agree) … THAT bad! Read the comments I inserted.

Fine! EVERYONE learns that way!

Regards,

DK