开发者

How do I create a redirect from a subdirectory to the root domain?

I am trying to redirect all requests to domain.com/drupal to domain.com, including all sub directories in /drupal.

I have seen several answers telling me how to accom开发者_Go百科plish the opposite of this with .htaccess, but nothing to go this way. I have tried the following line in .htaccess-

RewriteRule /drupal/* ^/(.*)

as well as several variations of the above, based on those answers, but haven't had any luck.

Thanks!


Try this line:

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


Let me get this straight ... You have an installation of Drupal in <DocumentRoot>/drupal/. You do not want to alter the drupal installation directory, nor you want to change DocumentRoot in your webserver config. You want to redirect any request, for example /foobar.php, into the drupal directory, resulting in maybe /drupal/foobar.php. And all that without exposing the whole stuff to the user. Right so far? OK, I can only assume that you have an Apache webserver, else .htaccess would not work...

First, make sure that you actually are allowed to use .htaccess, so check on the relevant AllowOverride directive in your apache config.

Then try it this way in your <DocumentRoot>/.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/drupal.*
RewriteRule ^/(.*)$ /drupal/$1 [P]

RewriteCond ensures that you do not run into an infinite loop. The first part of the RewriteRule is always the URL requested by the client. We prefix the part matched inside the parentheses with /drupal/ and force it to be a proxy request via [P] so that apache would only do an internal redirect (instead of sending the client a "Document has moved" redirection code).

BTW: I did not test it. I may have typos in the code. Read http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜