0

How can modrewrite be done in this above situation:

Lets say the website us is: www.real-estate.com

Then we have the first mod rewrite:

RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1

So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.

Then we add this rule:

RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2

This will rewrite to www.real-estates/florida/project-one and it will display details from that project.

But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.

What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?

Suciu Lucian
  • 430
  • 4
  • 12

2 Answers2

0

After playing around I found that his works, but I do not know if it is the correct solution:

I replaced this:

RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1

With this:

RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1

So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.

Suciu Lucian
  • 430
  • 4
  • 12
0

Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.

For your case use these rules:

RewriteEngine On
RewriteBase /

RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]

Apache mod_rewrite Introduction

anubhava
  • 761,203
  • 64
  • 569
  • 643