.htaccess rewrite to redirect root URL to subdirectory

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

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?

回答1:

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:

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:

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] 


回答4:

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.



回答5:

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:

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.



回答7:

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.



回答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/* 


回答9:

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] 


回答10:

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



回答11:

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.



回答12:

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



回答13:

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] 


回答14:

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.



回答15:

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/



回答16:

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):

 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]  
  1. Use RedirectMatch to only redirect the root URL “/” to another folder or URL,

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



回答17:

you just add this code into your .htaccess file

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!