如何从htaccess规则中排除给定的路由模式

时间:2019-07-11 12:09:58

标签: regex .htaccess

好的,所以我有一个网站- myweb.com ,然后我也有它的API托管在域的子目录- myweb.com/api 中。

当我尝试强制所有重定向到 https://www.myweb.com 时,我的API中断了,因为它已经使用了 https://myweb.com/api

以下是我目前的htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^ index.php [L]

    Redirect /page/faqs https://www.myweb.com/faqs
    Redirect /page/about https://www.myweb.com/about

    SetEnv TZ Africa/Lagos
</IfModule>

Options -Indexes

<Files .env>
    order allow,deny
    Deny from all
</Files>

<IfModule php5_module>
    php_flag asp_tags Off
    php_flag display_errors Off
    php_value max_execution_time 960
    php_value max_input_time 1920
    php_value max_input_vars 16000
    php_value memory_limit 16384M
    php_value post_max_size 384M
    php_value session.gc_maxlifetime 2880
    php_value upload_max_filesize 64M
    php_flag zlib.output_compression Off
</IfModule>

<IfModule mime_module>
    AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>

因此,基本上,我希望以下内容重定向至 https://www.myweb.com

但是,让以下内容不受上述规则约束:

1 个答案:

答案 0 :(得分:0)

更新的答案

现在,您已经提供了.htaccess并讨论了您的问题,在重定向规则中更早地处理/api请求可能会更容易。

在第一行RewriteEngine On之后添加此内容:

    # If handling an /api request ignore other rewrite rules.
    RewriteRule ^/api index.php [L]

这只会将/api请求传递给您的“前端控制器”,而不处理([L])之后的其他任何重写规则。


上一个答案

您可能想要类似于以下内容:

# Set %{ENV:PROTO} variable, to allow rewrites to redirect with the
# appropriate schema automatically (http or https).

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # No action if host already starts with "www."
    RewriteCond %{HTTP_HOST} !^www\. [NC]

    # No action if the path part that starts with "/api"
    RewriteCond %{REQUEST_URI} !^/api

    # Redirect to include "www."
    RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

您可以在GitHub上的Apache HTTP server boilerplate configs项目中找到类似的.htaccess示例。

您的HTTPS重定向将与此分开,您可能需要在此处查看特定示例: https://github.com/h5bp/server-configs-apache/blob/master/src/rewrites/rewrite_http_to_https.conf