Apache重写规则帮助,奇怪的结果

时间:2018-08-08 19:51:42

标签: php apache .htaccess s3-rewrite-rules

我在这方面盘旋。

RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]

第一行有效。当我输入/ Homes-by-price并显示

的页面时
 Real-Estate-Listings.php?minPrice=0&maxPrice=999999999

但是当我输入

 /Homes-by-price/100000 

我应该得到

 Real-Estate-Listings.php?minPrice=0&maxPrice=100000

相反,我得到了一个空白网页,并在调试控制台中显示了该页面的HTML脚本。

如果我输入

 /Real-Estate-Listings.php?minPrice=0&maxPrice=100000 

一切正常显示。

完整的htaccess(已删除注释掉的部分,以解决此问题。)

DirectoryIndex default.html

#Block listing of folder contents
IndexIgnore *

RewriteEngine on

#Rewrite rule for force https and www
# located in 000-default.conf


#rewrite rules single listing page (real-estate-listing.php)
#RewriteRule ^home-for-sale/mls/([0-9]+) real-estate-listing.php?mls=$1 [NC,L]
#RewriteRule ^home-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^commercial-property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^land-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^business-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]
#RewriteRule ^investment-property-for-sale/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+) real-estate-listing.php?city=$1&address=$2 [NC,L]

#rewrite rules for search page showing multiple listings (Real-Estate-Listings.php)
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
RewriteRule ^Homes-by-price Real-Estate-Listings.php?maxPrice=999999999 [NC,L]

<IfModule mod_headers.c>
  <FilesMatch ".(js|css|xml|gz|html|php|json)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

3 个答案:

答案 0 :(得分:0)

两个规则都应与第一个目标一起到达同一目标:

RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]
RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]

每个以Homes-by-price开头的请求都将由第一个规则处理,因此第二个规则无用,只需将最后一个规则放在开头就可以了:

RewriteRule ^Homes-by-price/([0-9]+) Real-Estate-Listings.php?minPrice=0&maxPrice=$1 [NC,L]
RewriteRule ^Homes-by-price Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 [NC,L]

像这样测试并让我知道

答案 1 :(得分:0)

不确定这是否是最好的解决方案,但确实可以解决问题。

通过在rewrite语句中使用完整的URL,它现在可以工作了。现在显示为:

RewriteRule ^Homes-by-price/([0-9]+)/([0-9]+) https://example.com/Real-Estate-Listings.php?minPrice=$1&maxPrice=$2 [DPI,NC,L]

答案 2 :(得分:0)

这是由您的第一条规则引起的。如果删除第一条规则,第二条规则将正常运行。您的第二条规则永远不会真正运行,这是因为第一条规则仍与URL匹配。因此它将更改为Real-Estate-Listings.php?minPrice=0&maxPrice=999999999,然后在末尾添加/100000。导致错误的原因。

从第一条规则中删除[L]应该应该解决此问题。由于第一个规则仍与URL匹配,因此它会运行,然后[L]标志会阻止其他任何规则通过它。

  

[L]标志使mod_rewrite停止处理规则集。在   在大多数情况下,这意味着如果规则匹配,则没有其他规则   将被处理。这对应于Perl中的最后一个命令,或者   C中的break命令。使用此标志指示当前   该规则应立即应用,而无需考虑其他规则。

有关RewriteRule标志如何工作的更多信息,请阅读以下文档:https://httpd.apache.org/docs/current/rewrite/flags.html