douxunzui1519
2013-10-29 09:55
浏览 73
已采纳

如果没有使用htaccess在URL中指定文件/目录,则重定向域

I have a subdomain images.example.com, which, as the name suggests, delivers images to all of the other subdomains on example.com.

At the moment, I have set up the virtual host to apply all custom error documents from the parent domain like so:

ErrorDocument 400 http://www.example.com/errors/400/
ErrorDocument 401 http://www.example.com/errors/401/
ErrorDocument 403 http://www.example.com/errors/403/
ErrorDocument 404 http://www.example.com/errors/404/
ErrorDocument 500 http://www.example.com/errors/500/
ErrorDocument 405 http://www.example.com/errors/405/
// And so on...

However, what I would like to do is redirect the visitor if they do not specify any file or directory. In other words, if they go to images.example.com, they will be redirected to www.example.com.

Example:

images.example.com will redirect to www.example.com

but

images.example.com/directory/ will not redirect

The reason why I only want to redirect the first page is simply because, if you do not specify a document/directory, it shows the default Apache Server Test page instead of showing an error page.

So, in short, how can I redirect a domain only if no file/directory has been specified in the URL?

Or, how can I prevent the default Apache Server Test page from showing when no file/directory has been specified?

Last Resort

If I am unable to achieve this using my .htaccess, then I will just add a php page to teh root directory or the images subdomain and then just redirect from there...

Further Information

I have read elsewhere that by specifying a 403 error document, then the Apache Server Test page will not be shown. Well this has not worked as I have specified the error and I am still seeing the test page...

图片转代码服务由CSDN问答提供 功能建议

我有一个子域 images.example.com ,顾名思义, 将图像传送到example.com上的所有其他子域。

目前,我已设置虚拟主机以应用来自父域的所有自定义错误文档,如下所示:

  ErrorDocument 400 http://www.example.com/errors/400/
ErrorDocument 401 http://www.example.com/errors/401/
ErrorDocument 403 http:  //www.example.com/errors/403/
ErrorDocument 404 http://www.example.com/errors/404/
ErrorDocument 500 http://www.example.com/errors/500/
ErrorDocument 405 http  ://www.example.com/errors/405/ 
 //等等...... 
   
 
 

但是,我想要做的是重定向 访问者,如果他们没有指定任何文件或目录。 换句话说,如果他们转到images.example.com,他们将被重定向到www.example.com。

示例:

\ n

images.example.com将重定向到www.example.com

但是

images.example.com/directory/不会重定向

我只想重定向第一页的原因仅仅是因为, 如果您没有指定文档/目录,它将显示默认的Apache Server Test页面,而不是显示错误页面。

因此,简而言之,如何 如果URL中没有指定文件/目录,我可以重定向域 吗?

或者,如何防止默认的Apache Server Test页面显示 什么时候没有指定文件/目录?

最后的手段

如果我无法使用我的.htaccess实现这一目的,那么我只会添加 一个php页面到根目录或图像子域,然后从那里重定向... 更多信息

我在其他地方读过,通过指定 403 错误文档,将不会显示Apache Server Test页面。 好吧,这没有用,因为我已经指定了错误,我仍然看到测试页...

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douping1993 2013-10-29 10:02
    最佳回答

    Line by line we have:

    1. turn rewrite engine on
    2. is the request a file that doesnt exist
    3. is the request a directory that doesnt exist
    4. If so then redirect to www.example.com, this is the last rule.

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ http://www.example.com [L]

    p.s. if images.example.com/directory/ will not redirect as it exists then images.example.com wont either.

    I suggest creating an index.php and do:

    header("Location:http://www.example.com");
    exit();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题