1

I have mydomain.com/residential/browse.php?region=1&town=2&suburb=3

I want to rewrite this to mydomain.com/residential/properties-for-sale-nsw-city-sydney.html

I want a php file to take the variables "region=1&town=2&suburb=3" and look up in a mysql database these variables and get the resulting names "nsw-city-sydney" for the mod rewrite (I can do this bit).

So, ultimately, I am trying to avoid creating a separate mod_rewrite entry in the .htaccess file for each combination of region/town/suburb, but rather somehow refer to a php file which, based on the GET variables, will provide the rewrite on the fly.

phperson
  • 27
  • 5

1 Answers1

0

Use mod_rewrite to send everything to a front controller (file), which then can interpret the query string, URL path, etc, and do whatever you want it to do.

Example .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This routes everything that isn't a request for an existing file to index.php.

jraede
  • 6,846
  • 5
  • 29
  • 32
  • Thankyou, so how do I get my rewrite file (which I call rewrite.php) to make mydomain.com/residential/browse.php?region=1&town=2&suburb=3 turn into mydomain.com/residential/properties-for-sale-nsw-city-sydney.html ??? I am still unclear on this bit. – phperson May 01 '14 at 17:57
  • Well, you can get the query string with `$_GET`. Then do your DB search for which region, town, suburb they correspond to, and serve the appropriate HTML file. – jraede May 01 '14 at 17:59
  • Or if you mean the opposite, you have to have your code parse the filename (`properties-for-sale-nsw-city-sydney.html`) and turn that in to`region=`&town=2&suburb=3`. – jraede May 01 '14 at 18:00
  • The real filename path is mydomain.com/residential/browse.php?region=1&town=2&suburb=3. So, does this mean that I need my PHP file to grab "nsw-city-sydney" and turn this into "region=1&town=2&suburb=3" ?? – phperson May 01 '14 at 18:08
  • Yep. You need a way to parse the URL and determine what that means in terms of your query. – jraede May 01 '14 at 19:38
  • Ok so assuming I setup my php file to interpret GET variables and then convert those to get the integers I need to build the string "region=1" etc then what do I do with this result? Do I just echo it onto the rewrite.php page? Do I need to build the whole URL or just the bit that gets appended after residential/ ? – phperson May 01 '14 at 19:51
  • Well no I misinterpreted your question initially. You'd have to get the request URI (`$_SERVER['REQUEST_URI']`), which would be `/residential/properties-for-sale-nsw-city-sydney.html` in your case. Then you'd need to parse that to retrieve `NSW`, `City`, `Sydney`, and all other relevant variables, convert them to IDs, and use them in your DB query. – jraede May 01 '14 at 19:55
  • I'm still unclear on what I then need to do once I have converted my text string values into integers? I want a user to be able to go to properties-for-sale-nsw-City-Sydney.html, which doesn't actually exist, but have the server use mod rewrite to translate this to browse.php?region=1 etc – phperson May 01 '14 at 20:10
  • And there are thousands of possible combinations of state, district, suburb, so I want the php file to dynamically do the rewrite, thus allowing any of these combinations to be valid. Make sense? I'm still unsure how to achieve this.... – phperson May 01 '14 at 20:12
  • Just to be completely clear, the user should not know that browse.php?region=1 etc exists, as if the rewrite is working correctly they will only navigate to and see the other URL, City-Sydney.html – phperson May 01 '14 at 20:14
  • Yes, all I can tell you is YOU need to find a naming convention that works and allows you to turn "properties-for-sale-nsw-city-sydney" into the appropriate database query and respond to the user with the appropriate data. – jraede May 01 '14 at 20:16
  • I am sorry, but this is still not making sense. – phperson May 01 '14 at 20:33
  • I know. So it is up to YOU to turn `properties-for-sale-nsw-city-sydney.html` INTO `browse.php?region=1&town=2&suburb=3` and either make a request to that URL (not recommended) or handle the request as if it's a request to that URL (recommended). – jraede May 01 '14 at 20:35
  • Ok, so I want to handle it as if it is a request for "browse.php?region=1&town=2&suburb=3" - how do I do this ? – phperson May 01 '14 at 20:38
  • Sigh. You're going to have to answer that on your own. Sorry. – jraede May 01 '14 at 20:43
  • All good, thanks for all your input :-) I am researching this now, looks good: http://stackoverflow.com/questions/16388959/url-rewriting-with-php – phperson May 01 '14 at 20:44