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...

  • 写回答

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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛