259

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?

Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101

19 Answers19

287

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]
  • 1
    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 Dec 29 '20 at 19:05
  • Where should I add this? – Jananath Banuka Feb 12 '21 at 04:22
165

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

Community
  • 1
  • 1
Dio Phung
  • 4,965
  • 2
  • 33
  • 48
  • this is simple and by far best answer – iamnabink Dec 15 '20 at 07: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 Dec 29 '20 at 18:12
  • Where should I add this? – Jananath Banuka Feb 12 '21 at 04:22
  • 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 https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch – Dio Phung Feb 24 '21 at 08:27
113

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]
Andrew
  • 12,513
  • 15
  • 53
  • 82
Gumbo
  • 594,236
  • 102
  • 740
  • 814
  • 5
    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. –  Jun 13 '09 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? – stefmikhail Oct 05 '11 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`? – stefmikhail Oct 05 '11 at 21:42
  • if this code used, can you type in your browser exmple.com/../aboveSubDir? –  Jul 27 '12 at 03:05
  • @GamErix This would be resolved to `/aboveSubDir` by either the browser or by the server. – Gumbo Jul 27 '12 at 05:47
  • @Gumbo, Could you please look into http://stackoverflow.com/questions/17937873/how-to-remove-a-subdirectory-from-the-url-when-forwarding-to-sudirectory?? – Starx Jul 30 '13 at 05:50
  • Should be the top solution, as it also shows how to external redirect as well. – Mangofett Aug 26 '18 at 23:50
  • My god after days, `RewriteRule ^$ web [L]` is what worked to redirect my symfony root (http://www.example.com/agenda) to its web subfolder where the app.php DirectoryIndex resides (http://www.example.com/agenda/web/[etc]). I'd love to take off that web bit but work for another day. – cdsaenz Feb 02 '21 at 00:42
  • Where should I add this? – Jananath Banuka Feb 12 '21 at 04:22
79

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.

Jage
  • 7,714
  • 3
  • 29
  • 30
34

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.

Amit Verma
  • 38,175
  • 19
  • 80
  • 104
24

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.

Andrew Barber
  • 37,547
  • 20
  • 91
  • 118
gaborous
  • 12,649
  • 7
  • 73
  • 94
  • 3
    Add `QSA` (query string append) to `[L]` (`[L, QSA]`) if you want query strings carried-over to the rewritten url. – devios1 Oct 10 '13 at 04: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 Mar 22 '14 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 – Geoffrey Hale Mar 24 '17 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 Dec 29 '20 at 19:16
  • 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 Dec 29 '20 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.

John Conde
  • 207,509
  • 96
  • 428
  • 469
user1801050
  • 161
  • 1
  • 2
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/*
inf3rno
  • 20,735
  • 9
  • 97
  • 171
8

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

Mahmoud Zalt
  • 24,048
  • 7
  • 74
  • 78
  • 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 Sep 23 '15 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 Sep 23 '15 at 11:16
  • All you need is to replace the words `example` and `store`. – Mahmoud Zalt Sep 24 '15 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 Sep 24 '15 at 15:53
  • Well my answer above does not do that. Try posting a new question. – Mahmoud Zalt Sep 25 '15 at 17:11
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]
Nir O.
  • 1,322
  • 1
  • 16
  • 25
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.

Chetabahana
  • 7,806
  • 2
  • 50
  • 70
4

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

Abhishek Gurjar
  • 7,152
  • 9
  • 35
  • 40
  • 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 Dec 30 '20 at 18:33
4

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

Mushtaq Hussain
  • 681
  • 6
  • 5
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]
Artem Russakovskii
  • 20,170
  • 17
  • 87
  • 114
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.

Steven
  • 18,168
  • 44
  • 141
  • 240
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/

Abouasy
  • 706
  • 5
  • 7
0

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>
karthikeyan ganesan
  • 1,120
  • 11
  • 17
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.

w5m
  • 2,259
  • 3
  • 29
  • 42
0

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]
mikeytown2
  • 1,636
  • 23
  • 37