Apache rewrite rule help, strange results

63 Views Asked by At

I am going in circle on this.

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]

The first line works. When I enter /Homes-by-price and get the page showing

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

But when I type in

 /Homes-by-price/100000 

I should get

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

Instead I get a blank web page and the debug console showing me the HTML script for the page.

If I type in

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

everything displays correctly.

Complete htaccess (The commented out portions where removed for trouble shooting this issue.)

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

There are 3 best solutions below

0
On

Not sure if this is the best solution, but it did resolve the issue.

By use the full URL in the rewrite statement it now works. It now reads:

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

This is being caused by your first rule. If you remove the first rule, the second rule runs normally. Your second rule is never actually getting run, this is because the first rule still matches the URL. So it makes the change to Real-Estate-Listings.php?minPrice=0&maxPrice=999999999 and then adds /100000 on to the end. Which is causing the error.

Removing the [L] from the first rule should fix this. Since the first rule is still matching the URL it runs and then the [L] flag stops any further rules from being run past it.

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

For more information on how the RewriteRule Flags work, you read the documentation here: https://httpd.apache.org/docs/current/rewrite/flags.html

5
On

Both rules should go to same target with first target :

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]

Every request starts with Homes-by-price will be handled by the first rule so the second rule is useless, just put the last rule in the beginning like this :

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]

Test it like this and let me know