【问题标题】:Multiple htaccess rewrites with subdomains & execptions使用子域和异常进行多次 htaccess 重写
【发布时间】:2015-04-02 01:34:11
【问题描述】:

我有一套复杂的重写规则,我试图简化这些规则,但总是陷入困境。我的目标如下:

  1. 通过 www 将所有流量重定向到网站(子域“test”和“dev”除外)
  2. 通过 https 重定向所有流量
  3. 使用 url 字符串作为参数,通过该目录中的文件重定向通过特定目录(“webservice”)的所有流量

到目前为止,我有 2 个 htaccess 文件,一个在我的 web 根目录中,另一个在我的 webservice 目录中。

我的根 htaccess 如下所示:

RewriteEngine on
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !^(dev|test)\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^(dev|test)\. [NC]
#RewriteCond %{REQUEST_URI} !^/?(webservice)/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

到目前为止,这工作正常,并通过 www 和 https 正确重定向了我的大部分流量。

我的第二个 htaccess 文件在 /webservice/rest 中如下所示:

RewriteEngine On
RewriteBase /webservice/rest
RewriteRule ^([\w-]+)/?(\?([\w-=?&]))?$ api_client.php?method=$1&params=$2 [L,NC]

这会正确地将我基于 url 的 api 参数重定向到 /webservice/rest ('api_client') 中的正确文件中。

这与我设法获得的最终解决方案一样接近。剩下的问题是指向“webservice”url的url没有重定向到www或https(我也希望不重定向指向/webservice/rest文件夹的“test”和“dev”子域作为例外)

在维护我的最终 web 服务 htaccess 参数重定向的同时如何做到这一点有什么想法吗?

【问题讨论】:

    标签: php web-services .htaccess redirect apache2


    【解决方案1】:

    您需要在子目录的 htaccess 文件中包含相同的 www/https 重定向,因为它取代了父目录中的规则。

    RewriteEngine On
    RewriteBase /webservice/rest
    
    # Ensure www on all URLs.
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteCond %{HTTP_HOST} !^(dev|test)\. [NC]
    RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
    
    RewriteRule ^([\w-]+)/?(\?([\w-=?&]))?$ api_client.php?method=$1&params=$2 [L,NC]
    

    【讨论】:

    • 谢谢乔恩 - 这更接近但不完全在那里。如果我进行此更改,我必须在我的第一个代码块中取消注释该行 - "RewriteCond %{REQUEST_URI} !^/?(webservice)/" 并且它们几乎都挂在一起,除非我请求以简单示例开头的 API。 com(而不是 www)我最后的重写规则没有正确执行参数 - 最终产品看起来像 example.com/webservice/rest/…
    • 另外 - 我必须在我的第一个代码块中取消注释该行,否则我会被重定向到 404
    • 从头开始,Jon - 这对我有用,一定只需要几分钟就可以完全传播。对于其他偶然发现此问题的人,我需要使用 RewriteCond %{REQUEST_URI} !^/?(webservice)/ 排除我的子目录以使其正常工作,并将我的所有障碍添加到第二个 htaccess 文件中 - 也许其他人可以阐明原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多