【问题标题】:URL Rewrite of Non-Existent File Under Subdomain Folder子域文件夹下不存在文件的 URL 重写
【发布时间】:2014-02-08 03:23:13
【问题描述】:

我的网站结构如下:

.htaccess
index.html
internal
   .htaccess
   test.html
   api.php

我在 GoDaddy 中设置了一个子域,以便 internal.ledworld-me.com 映射到 ledworld-me.com/internal 并且以下工作正常:

请求 internal.ledworld-me.com/test.html 正确地为 test.html 文件提供服务。

现在我需要以下内容:

如果请求internal.ledworld-me.com/nonexistent/file,则应将URL 重写为ledworld-me.com/internal/api.php?__route__=/nonexistent/file

为了实现这一点,我在我的.htaccess 文件中添加了什么?我应该将代码放在哪个.htaccess 文件中,WebRoot 下的那个还是internal 下的那个?

请注意,GoDaddy 负责将 internal.ledworld-me.com 重写为 ledworld-me.com/internal。我需要我的.htaccess 文件做的是重写的第二部分,即/nonexistent/file 应该重写为api.php?__route__=/nonexistent/file

这个问题真的很难解释,我已经尽力了,如果需要,请要求澄清。谢谢!

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting subdomain


    【解决方案1】:

    将此代码放入您的 /internal/.htaccess 文件

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/?$ api.php?__route__=/$1 [L,QSA]
    

    【讨论】:

    • 这会导致 404 未找到并显示消息“在此服务器上未找到请求的 URL /internal/nonexistent/file。”
    【解决方案2】:

    我的 internal/.htaccess 文件中的以下文本似乎可以解决问题:

    RewriteEngine on                                                                          
    RewriteCond %{REQUEST_FILENAME} !-f                                                       
    RewriteCond %{REQUEST_FILENAME} !-d                                                       
    RewriteRule ^(.*)$ http://ledworld-me.com/internal/api.php?__route__=/$1
    

    我仍在测试所有场景,并在完成所有测试后确认一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      相关资源
      最近更新 更多