Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite help please

Its probably easy to solve - but I'm stuck!

         

Operandi

9:10 pm on Jun 14, 2005 (gmt 0)

10+ Year Member



Hi

I want to mimic static URLs with a mod_rewrite solution.

For example, I want the URLs to be:

mydomain.com/new_york which maps onto town.php?town=new_york

and then

mydomain.com/new_york/theaters which maps onto category.php?town=new_york&category=theaters

The problem is that I need to tell Apache which script to process
the directive - town.php or category.php

I normally get around this by these rules:

ReWriteRule ^town/([^/]+).html$ town.php?town=$1 [L,NC]
ReWriteRule ^category/([^/]+)/([^/]+).html$ category.php?town=$1&category=$2 [L,NC]

but this creates the URL's like this:

mydomain.com/town/new_york.html

and mydomain.com/category/new_york/theaters.html

I have tried these rules, but they ended up in an endless loop:

ReWriteRule ^([^/]+)/$ town.php?town=$1 [L,NC]
ReWriteRule ^([^/]+)/([^/]+).html$ category.php?town=$1&category=$2 [L,NC]

Can anybody help please as I'm stuck?

Thanks very much

jd01

11:04 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Operandi,

Welcome to WebmasterWorld.

You will need to change the links on your page. Mod_Rewrite does not create URL's it, either serves information from a different file to the location requested, or redirects the browser to a different location.

After you change your URL's to the format you would like, you will need to adjust your rules, for the example you used, the following should work:

ReWriteRule ^town/([^/.]+)$ town.php?town=$1 [L,NC]
ReWriteRule ^category/([^/]+)/([^/.]+)$ category.php?town=$1&category=$2 [L,NC]

([^/.]+) = 1 or more of anything that is not a / or a .(dot).

Hope this helps.

Justin

jdMorgan

2:39 am on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem description above isn't real clear, probably because this subject is tricky, but part of what I'm reading into this is that "category" and "town" are not desired in the 'friendly' URL. So the problem is one of:

  • Passing single-parameter-URL requests to "/town"
  • Passing two-parameter-URL requests to "/category"

    And in that case, tweaking the rules just a bit gives:


    RewriteRule ^([^/.]+)$ /town.php?town=$1 [NC,L]
    RewriteRule ^([^/]+)/([^/.]+)$ /category.php?town=$1&category=$2 [NC,L]

    Notice that Justin's (clever) inclusion of "." as a negative match character is what stops the rewrite looping, because "town.php" and "category.php" both contain periods, and so cannot be repeatedly rewritten.

    A further tweak may be needed if the requested URLs might have trailing slashes:


    RewriteRule ^([^/.]+)/?$ /town.php?town=$1 [NC,L]
    RewriteRule ^([^/]+)/([^/.]+)/?$ /category.php?town=$1&category=$2 [NC,L]

    The explanation of mod_rewrite's function above is spot-on, and understanding that helps make this process easier to think about.

    Jim

  • Operandi

    10:32 am on Jun 15, 2005 (gmt 0)

    10+ Year Member



    Hi

    Thanks to you both for piunting me in the right direction.

    Jim you are correct - I am trying to remove the requirement for town and category from the URL.

    Using your examples I could get the querystrings re-written, however I now have problems with all the other PHP scripts in the same directory as they now fail, presumably as they are subject to the re-write rules also.

    Is there a way of excluding these other files from the rewrite rules?

    I appreciate your help.

    g1smd

    2:25 pm on Jun 15, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




    Please do yourself a favour and replace each underscore in the URL with either a hyphen or a dot.

    Try to avoid using underscores or spaces in any URL. They do cause lots of problems.

    jdMorgan

    2:51 pm on Jun 15, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Operandi,

    We need a lot of detail in order to offer solid advice or solutions. If you mention that you have 'other scripts' that fail, but don't provide any example URLs or script names, then that leaves us wondering why they get rewritten, since the above code won't rewrite any URL with a period in it. We also might wonder what the specific nature of the failure is, e.g. the other scripts don't run because requests to them are diverted to the town or category script instead, or those other scripts fail because they don't get needed parameters passed to them, or they fail because they can no longer find needed resources whose URL=paths have been rewritten, etc.

    The result of this is that anyone who might help you must post to ask you a clarifying question, and we can go back and forth for weeks, and you will lose the interest of many qualified-but-busy respondents. So please post as much detail as possible, and provide specific examples rather than generalities. The more work you do, the less is required of those who might help, and the more likely you will get a satisfactory solution. Our forum charter (link at upper left) offers some tips in this regard.

    The apparent problem here is that you will need an expanded list of RewriteRule exclusions (See RewriteCond directive). It's a theoretically-easy problem to fix, but the correct solution depends on the details.

    Jim