Forum Moderators: phranque

Message Too Old, No Replies

htaccess help

         

eggenz

7:39 am on Jun 4, 2007 (gmt 0)

10+ Year Member



hi and thanks for takin the time to read thes message

ive tryed and tryed to get things working on my site with no sucess
so im pleeding for some help :)

my site www.mydomain/listing.php?cityname=3

where cityname3 = to Sydney

aswell as www.mydomain/listing.php?Catid=11&cityname=3

where catid11 = to cafe

so in the ideal world i would like-

www.mydomain/listing.php?Catid=11&cityname=3

to read

www.mydomain/cafe/sydney

is this even possible and if so ANY help is appreciated

thanks in advance eggenz

jdMorgan

12:53 am on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's hard to answer your question with so little background information, but a good way to do it is:

  • Modify your script to output the on-page links (URLs) as "/Sydney/cafe"
  • Add code to mod_rewrite to internally rewrite any requested URLs with no file extension (as above) to /index.php
  • Add code to index.php to look up the required parameters cityname=3 and catid=11, and then proceed as before
  • Finally, you can optionally write some code to rewrite all cityname=3&catid=11-format URLs to the script, and add code to the script externally 301-redirect those requests to the appropriate friendly URLs. This will "fix" search engine listings so that pages will be listed with friendly URLs instead of the original dynamic URLs.

    In other words, most of the work needs to happen where you have access to the database that can be used to translate "3" to Sydney and "11" to cafe -- in the script itself.

    You *can* do this using mod_rewrite's RewriteMap directive, but you still need a server-side script (e.g. PERL) to access the database to translate the words in requested URLs to the name/value pairs needed to invoke your .php script, and you still need to modify the original PHP script to translate the name/value pairs back to words to generate the friendly on-page links. So generally, it's less work to do it all in the original script.

    To rewrite all extensionless URLs to your script, something like:


    RewriteRule ^(([^/]+/)*([^./])?)$ /index.php?URL-path=$1 [L]

    This will pass the entire requested local URL-path to the script as "URL-path=<entire-path>" as long as the final URL-path-part does not contain a period or a slash. This also prevent index.php from being rewritten to itself recursively, creating an 'infinite' loop.

    The result of requesting example.com/Sydney/cafe from your server will be a call to index.php with a query string of "URL-path=Sydney/cafe". This is a internal filepath, and not a URL; It *will not* be visible to users or to search engines.

    You could also simply access the local URL-path as a server variable from within your script itself.

    Jim

  • phranque

    12:58 am on Jun 5, 2007 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



    welcome to WebmasterWorld, eggenz!

    you might be able to solve this using the RewriteMap directive of the apache mod_rewrite module [httpd.apache.org]

    jdMorgan

    2:37 am on Jun 5, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    As noted above, using RewriteMap requires a secondary script (or a secondary lookup table); Since there's already a script being used with a database, it's probably much easier to do all the required work using a modified version of the existing script and the existing data, rather than creating additional and redundant code and data which will have to be maintained...

    Jim