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。
但是,让以下内容免于上述规则:
解决方案
更新的答案
现在您已经提供并讨论了您的问题,在您的重定向规则中更早.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 服务器样板配置项目中找到类似.htaccess
的示例。
您的 HTTPS 重定向将与此分开,您可能希望在此处查看特定示例: https ://github.com/h5bp/server-configs-apache/blob/master/src/rewrites/rewrite_http_to_https.conf
推荐阅读
- pandas - 将数据框乘以列值
- javascript - 如何使用 JavaScript 对象作为过滤条件?
- .net-core - .Net Core 与 WS-Federation 生成大型 wctx
- c++ - 试验线程优先级 C++
- r - R:有人能在我的对数转换(多元线性回归)中发现错误吗?
- c - 查找 int 数组 C 中的所有回文
- arrays - NumPy 数组的最小值和最大值都是 NaN
- javascript - 如何使用 fetch 将数据发布到 php
- php - Symfony 信使队列与绑定键 - 重试策略
- c - 无法使用结构来存储和稍后使用 recvfrom 打印 udp 数据