18

Note: I have tried the suggestions under this question but they either don't work or give me an infinite redirect loop error.

I have the following in my .htaccess file:

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    # Rewrite old links to new
    Redirect 301 /howitworks.php        /how-it-works
    Redirect 301 /testimonials.php      /testimonials
    Redirect 301 /contact_us.php        /customer-service
    Redirect 301 /affiliates.php        /affiliates
    Redirect 301 /account.php           /customer/account
    Redirect 301 /shopping_cart.php     /checkout/cart
    Redirect 301 /products.php          /starter-kits
    Redirect 301 /login.php             /customer/account/login

    # Force to Admin if trying to access admin
    RewriteCond %{HTTP_HOST} www\.example\.com [NC]
    RewriteCond %{REQUEST_URI} admin [NC]
    RewriteRule ^(.*)$ https://admin.example.com/index.php/admin [R=301,L,QSA]

    # Compress, Combine and Cache Javascript/CSS
    RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d=$2

    # Always send 404 on missing files in these folders
    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

    # Never rewrite for existing files, directories and links
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI} !server-status

    # Rewrite everything else to index.php
    RewriteRule .* index.php [L]

    # Force www in url and non-example domains to example
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^ww2\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^qa\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^uat\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^local\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^admin\.example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]

</IfModule>

I need a rewrite that will do the following:

Rewrite: http://subdomain.example.com/ to http://subdomain.example.com/page

I would also like this to hide the /page (/ would act as if it was /page) but this is not a necessary requirement. Also, I'd like it to work with HTTP or HTTPS.

Any help is greatly appreciated because I have been struggling with this for hours.

Community
  • 1
  • 1
leek
  • 11,803
  • 8
  • 45
  • 61

3 Answers3

26

A correct redirect of the root path is simply :

RewriteEngine On
RewriteRule ^/$ /page [R]

to force this only on one subdomain (which should barely happen if your rewrite block is in a virtualhost definition) :

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^/$ /page [R]
sberder
  • 4,505
  • 2
  • 26
  • 15
  • 8
    This answer simply doesn't work in htaccess context, and does a full redirect to the client (IOW doesn't "hide" as asked) – covener Dec 30 '12 at 21:08
  • 4
    This only worked for me on apache2 once I used ^/$ instead of ^$ . Is the answer incorrect or am I making a mistake? – Konstantin Schubert Aug 01 '14 at 13:44
  • 1
    Wrong: RewriteRule ^$ /page [R] Right: RewriteRule ^/$ /page [R] – Hartmut Pfarr Apr 25 '16 at 18:51
  • 1
    @KonstantinSchubert If this worked for you then you probably arnt using htaccess. The pattern `^/$` works on `server.config` context. .htaccess uses a relative path pattern that starts without a leading slash. `^$` is a valid pattern to match against the hompage using RewriteRule in htaccess. – Amit Verma Apr 27 '20 at 15:27
  • Worked using `^$` instead of `^/$` in `.htaccess` file – Vasiliy Artamonov Jul 15 '21 at 11:52
7

Copying my deleted response:

In htaccess of your documentroot simply:

RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^$ /page
covener
  • 17,402
  • 2
  • 31
  • 45
  • 3
    Not sure why this got deleted... `RewriteRule ^$ /page` seems to be the only valid answer. `RewriteRule ^/$ /page` does _NOT_ match a request to the document root of a site. I'm modifying the currently accepted answer so that it is correct. – joshperry Nov 06 '13 at 00:05
3

You have two options to rewrite your root to another path,

  1. DirectoryIndex:

    DirectoryIndex anotherPath
    
  2. Mod-rewrite:

    RewriteEngine on
    RewriteRule ^/?$ /anotherPath [L]
    

You can use one of the above options to internally redirect example.com/ to example.com/anotherPath.

References:

  1. https://httpd.apache.org/docs/current/mod/mod_dir.html
  2. http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Amit Verma
  • 40,709
  • 21
  • 93
  • 115