修改现有的.htaccess,使其忽略子域

时间:2016-11-18 11:18:48

标签: apache .htaccess

我有以下.htaccess文件到位,虽然我知道它远远不是最佳(并且可能有点畏缩) - 它只是有效。

# Prevents public viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Custom 404 page
ErrorDocument 404 https://www.domain.com/404/

# Turn on rewriting and set rewriting base
RewriteEngine On
RewriteBase /

# Force WWW
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Force WWW and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]

# Remove .php extension
RewriteRule ^(properties)/(all|location1|location2|location3)/(all|1|2|3|4|5|6|7|8|9)/(asc|desc) $1.php?location=$2&bedrooms=$3&sort-by=$4
RewriteRule ^(properties)/(all|location1|location2|location3)/(all|1|2|3|4|5|6|7|8|9) $1.php?location=$2&bedrooms=$3
RewriteRule ^(properties)/(all|location1|location2|location3) $1.php?location=$2
RewriteRule ^(view-property)/(.*)/(print-view) $1.php?id=$2&print=true
RewriteRule ^(view-property)/(.*)/ $1.php?id=$2
RewriteRule ^(.*)/$ $1.php

我们的新网站现已准备好推送到我们新创建的临时环境进行测试。在这种情况下,它将是staging.domain.com但我们在实际访问此暂存URL时遇到问题。

编辑 - 只是为了澄清,问题是访问staging.domain.com重定向到https://www.domain.com

我们认为问题是由我们上面的重写规则引起的。我研究了一些可能的解决方案,包括为现有的重写规则添加其他条件,例如:

RewriteCond %{HTTP_HOST} !^staging\.domain\.com

或添加新条件/规则,例如:

RewriteCond %{HTTP_HOST} ^staging\.domain\.com [NC]
RewriteRule ^(.*) - [L]

但不幸的是,这些都没有奏效。使用.htaccess文件并不是我的强项,所以如果有人能提供任何帮助,那就太棒了。

P.S。新网站由Laravel 5.3提供支持,因此当我们上线时,我将摆脱重写规则(底部的规则)的可怕尝试!

1 个答案:

答案 0 :(得分:1)

您的最高规则必须更改为此目标才能删除目标网址中的硬编码域名:

# Turn on rewriting and set rewriting base
RewriteEngine On
RewriteBase /

# Force WWW and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# Remove .php extension and rest of the rewrite rules
相关问题