Forum Moderators: phranque
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
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
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]
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]
Jim
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.
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