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



我要在这个问题上兜圈子。

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]

第一行有效。当我输入/按价格计算房屋并显示页面时

 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>

两个规则都应该与第一个目标一起去同一个目标:

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]

像这样测试它,让我知道

不确定这是否是最佳解决方案,但它确实解决了问题。

通过在重写语句中使用完整的 URL,它现在可以工作了。它现在的内容是:

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

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

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

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

有关重写规则标志如何工作的更多信息,请阅读此处的文档:https://httpd.apache.org/docs/current/rewrite/flags.html

最新更新