283

Trying to get

www.example.com

to go directly to

www.example.com/store

I have tried multiple bits of code and none work.

What I've tried:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

What am I doing wrong?

0

19 Answers 19

306

You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your /store directory, like this:

RewriteEngine On
RewriteRule ^$ /store [L]
2
  • 5
    If /store is a physical subdirectory then you will need to append a trailing slash on the RewriteRule substitution, otherwise mod_dir will trigger a 301 external redirect to append the trailing slash and /store/ will be exposed to your users. Also, the regex ^$ won't "appear to" match if you have a DirectoryIndex document (eg. index.html) in the root directory (nothing "appears to" happen) since mod_dir will issue an internal subrequest for the index document after the initial rewrite. To get around both these issues, change it to RewriteRule ^(index\.html)?$ /store/ [L].
    – MrWhite
    Commented Dec 29, 2020 at 19:05
  • If you may add a real page at the root level at later date you should use a temporary redirect by replacing the [L] with [R=302,L] Commented Aug 28, 2022 at 17:51
190

I was surprised that nobody mentioned this:

RedirectMatch ^/$ /store/

Basically, it redirects the root and only the root URL. The answer originated from this link

3
  • this is simple and by far best answer
    – iamnabink
    Commented Dec 15, 2020 at 7:02
  • 1
    @iamnabink That depends if you want an internal rewrite (as stated in the question and as answered by the earlier answers) or a 302-temporary external redirect (as stated here). It also depends whether you have existing mod_rewrite directives, as the OP clearly has. So, in the context of the question it's not the "best answer". YMMV.
    – MrWhite
    Commented Dec 29, 2020 at 18:12
  • Add this line in the .htaccess file under the web app root (for e.g /var/www/html_docs/.htaccess) if you don't have access to server configuration file. Else you can modify the server configuration, see more httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch
    – Dio Phung
    Commented Feb 24, 2021 at 8:27
117

Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]

If you want an external redirect (which cause the visiting browser to show the redirected URL), set the R flag there as well:

RewriteRule ^$ /store [L,R=301]
9
  • 6
    That worked beautifully. Both the sample.com and www.sample.com redirect to www.sample.com/store. That's just what I wanted. Thank you Gumbo, and everyone who answered. I learned a lot reading your responses and appreciate the feedback.
    – AlphaSmith
    Commented Jun 13, 2009 at 10:09
  • 1
    I'm curious, will this show www.example.com as the URI after the redirect? If not, how would it be changed to do so? Commented Oct 5, 2011 at 21:05
  • 1
    ♦ - And why would your solution be better than Sander's down below? I notice your final line is the same, but if that is all that is needed, why include the RewriteCond and the first RewriteRule? Commented Oct 5, 2011 at 21:42
  • if this code used, can you type in your browser exmple.com/../aboveSubDir?
    – user1182183
    Commented Jul 27, 2012 at 3:05
  • @GamErix This would be resolved to /aboveSubDir by either the browser or by the server.
    – Gumbo
    Commented Jul 27, 2012 at 5:47
82

Here is what I used to redirect to a subdirectory. This did it invisibly and still allows through requests that match an existing file or whatever.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]

Change out site.com and subdir with your values.

0
44

To set an invisible redirect from root to subfolder, You can use the following RewriteRule in /root/.htaccess :

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]

The rule above will internally redirect the browser from :

to

And

to

while the browser will stay on the root folder.

1
  • 1
    This answer works well if you need a redirect that is invisible to the user.
    – Fred
    Commented Jan 2, 2022 at 18:24
26

Another alternative if you want to rewrite the URL and hide the original URL:

RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]

With this, if you for example type http://www.example.com/product.php?id=4, it will transparently open the file at http://www.example.com/store/product.php?id=4 but without showing to the user the full url.

6
  • 3
    Add QSA (query string append) to [L] ([L, QSA]) if you want query strings carried-over to the rewritten url.
    – devios1
    Commented Oct 10, 2013 at 4:53
  • @chaiguy thank's for the tip, but on my server it worked without QSA (but I agree it's probably better to append QSA). Maybe it's because of some special config of my apache server?
    – gaborous
    Commented Mar 22, 2014 at 14:20
  • 1
    This method for me, example.com shows content from example.com/store transparently but example.com/test redirects to example.com/store/test, showing the url Commented Mar 24, 2017 at 20:31
  • 1
    @devios1 You don't need the QSA flag in this example since there is no query string in the substitution string. When there is no query string in the substitution the original query string from the request is passed through by default.
    – MrWhite
    Commented Dec 29, 2020 at 19:16
  • 1
    This example will result in a rewrite-loop (500 error response) unless you also have another .htaccess file in the /store subdirectory that also contains mod_rewrite directives (since mod_rewrite directives are not inherited by default, thus preventing a rewrite-loop).
    – MrWhite
    Commented Dec 29, 2020 at 19:18
16

This seemed the simplest solution:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]

I was getting redirect loops with some of the other solutions.

0
11

Most of the above solutions are correct but they are all missing the transparency of the redirection.

In my case, when visiting www.example.com I wanted to get redirected to the subdirectory /store but without updating the URL to www.example.com/store. (all I want is to get the page code form that directory). If that is your case the solution below works perfectly.

RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /store/$1 [L]

source: http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory

5
  • What should I change if I needed that www.example.com got redirected to www.othersite.com/store without updating the URL? I tried but was unable to adapt your code
    – steps
    Commented Sep 23, 2015 at 11:10
  • I tried to to change the last line to RewriteRule ^(.*)$ http://otherpage.com/subfolder/$1 [L] but it updates the URL
    – steps
    Commented Sep 23, 2015 at 11:16
  • All you need is to replace the words example and store. Commented Sep 24, 2015 at 14:14
  • I didn't explain quite well. I own example.com and otherpage.com domains. When I visit example.com I want that the contents from otherpage.com/subfolder to be displayed, without updating the URL
    – steps
    Commented Sep 24, 2015 at 15:53
  • Well my answer above does not do that. Try posting a new question. Commented Sep 25, 2015 at 17:11
8

I don't understand your question...

If you want to redirect every request to a subfolder:

RewriteRule ^(.*)$ shop/$1 [L,QSA]

http://www.example.com/* -> wwwroot/store/*

If you want to redirect to a subfolder which has the domain name

RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [L,QSA]

http://www.example.com/* -> wwwroot/example.com/*
1
  • I like your code ([^\.]+\.[^\.]+)$ to reserve domain as subfolder name.
    – eQ19
    Commented Feb 16, 2016 at 15:53
7

I have found that in order to avoid circular redirection, it is important to limit the scope of redirection to root directory. I would have used:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
5

Two ways out of possible solutions to achieve this are: 1. Create a .htaccess file in root folder as under (just replace example.com and my_dir with your corresponding values):

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_dir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_dir/index.php [L] 
</IfModule>
  1. Use RedirectMatch to only redirect the root URL “/” to another folder or URL,

    RedirectMatch ^/$ http://www.example.com/my_dir

4

Formerly I use the following code which is work correctly to redirect root URL of each of my domains/subdomains to their correspondence subdirectories which are named exactly as the sub/domain it self as below:

RewriteCond %{HTTP_HOST} ^sub1.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain1.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^sub2.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^sub1.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^sub2.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub2.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]

However when I want to add another subs or domains then it will need to be added in the above code. It should be much more convenient to simplify it to work like wildcard (*) as below:

RewriteCond %{HTTP_HOST} ^sub
RewriteCond %{REQUEST_URI} !/subs/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]

So whenever another subdomains/domains is added as long as the subdomain name has a prefix of sub (like: sub3.domain1.com, sub1.domain3.com etc.) the code will remain valid.

3

I think the main problems with the code you posted are:

  • the first line matches on a host beginning with strictly sample.com, so www.sample.com doesn't match.

  • the second line wants at least one character, followed by www.sample.com which also doesn't match (why did you escape the first w?)

  • none of the included rules redirect to the url you specified in your goal (plus, sample is misspelled as samle, but that's irrelevant).

For reference, here's the code you currently have:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^sample.com$
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.sample\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
3

One can use Redirect too for this purpose

Redirect 301 / www.example.com/store

Or Alias for mapping

Alias / /store

Edit: mod_alias is only applicable in httpd.conf.

Refrences

1
  • You can't use Redirect here, it will result in a redirect loop. (You could use RedirectMatch, as mentioned in other answers.) Also, the Alias directive will need a trailing slash on the file-path (2nd argument), ie. Alias / /store/, otherwise a request for /foo will map to /storefoo instead of /store/foo.
    – MrWhite
    Commented Dec 30, 2020 at 18:33
2

A little googling, gives me these results:

RewriteEngine On
RewriteBase /
RewriteRule ^index.(.*)?$ http://domain.com/subfolder/ [r=301]

This will redirect any attempt to access a file named index.something to your subfolder, whether the file exists or not.

Or try this:

RewriteCond %{HTTP_HOST} !^www.sample.com$ [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/samlse/$1 [R=301,L]

I haven't done much redirect in the .htaccess file, so I'm not sure if this will work.

1

try to use below lines in htaccess

Note: you may need to check what is the name of the default.html

default.html is the file that load by default in the root folder.

RewriteEngine

Redirect /default.html http://example.com/store/

1

you just add this code into your .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /folder/index.php [L]
</IfModule>
1

This will try the subdir if the file doesn't exist in the root. Needed this as I moved a basic .html website that expects to be ran at the root level and pushed it to a subdir. Only works if all files are flat (no .htaccess trickery in the subdir possible). Useful for linked things like css and js files.

# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]
0

I'll answer the original question not by pointing out another possible syntax (there are many amongst the other answers) but by pointing out something I have once had to deal with, that took me a while to figure out:

What am I doing wrong?

There is a possibility that %{HTTP_HOST} is not being populated properly, or at all. Although, I've only seen that occur in only one machine on a shared host, with some custom patched apache 2.2, it's a possibility nonetheless.