Forum Moderators: phranque
I need
http://www.example.com/New_York/Long_Beach-The_Meridian-2_Bedroom-Condo-11.html
Written to:
http://www.example.com/New_York/Long_Beach--The_Meridian--2_Bedroom--Condo--11.html
the variables passed on my site:
/New_York/".str_replace(" ", "_", $city)."--".str_replace(" ", "_", $title)."--".str_replace(" ", "_", $bed)."_Bedroom--".str_replace(" ", "_", $property_type)."--".$id.".html
I cant grasp what needs to be done. But I know its a fast fix. Anybody willing to help. I can't find any good tutorials online.
[edited by: jdMorgan at 5:22 pm (utc) on Sep. 17, 2008]
[edit reason] example.com [/edit]
In short, mod_rewrite is a lousy solution for this kind of problem, and you would be better off trying to find another approach to URL design in order to avoid the problem completely.
If you do decide that you really have to use this approach, then in order to avoid the Apache bug, you'll need to copy the requested URL into a 'user-defined' variable, work on it inside that variable, and finally rewrite once using that variable as the substitution URL. It's complex, inefficient, and very messy.
Jim
Is there any online tutorials that explains rewrite conditions for a 'Dummy'
I got the rewrite part down pat. Not its the Re-writting a re-written URL that i have trouble with.
The Apache documentation on mod_rewrite is the place to start. Look at some of the code in the posts in this forum (and others), and refer to the documentation to "decode" them and understand them character-by-character.
Here's an example to get started... :)
As I said, this can be done with fewer rules, but it's ugly and inefficient, since the work must be done in a user variable to avoid the multiple-sequential-rewrite bug in Apache. Here's an example (untested) for up to four hyphens in the requested URL:
# If URL contains any hyphen not followed by another hyphen, do one
# replacement and put the resulting URL-path into "myURL" variable
RewriteRule ^([^\-]+-)([^\-].*)$ - [E=myURL:$1-$2,C]
#
# For up to three hyphen replacements, include this rule once.
# For up to four, repeat it two times (as shown here), etc.
RewriteCond %{ENV:myURL} ^([^\-]+-)([^\-].*)$
RewriteRule . - [E=myURL:%1-%2,C]
#
RewriteCond %{ENV:myURL} ^([^\-]+-)([^\-].*)$
RewriteRule . - [E=myURL:%1-%2,C]
#
# Now do a final repeat, but without a [C] flag, so
# that the last rule (below) will always execute
RewriteCond %{ENV:myURL} ^([^\-]+-)([^\-].*)$
RewriteRule . - [E=myURL:%1-%2]
#
RewriteRule ^[^\-]+-[^\-].*$ /%{ENV:myURL} [L]
If all of your URLs that need to be rewritten start with "New_York", then you should either move this code into that subdirectory (it it actually exists) or add "New_York" within the parentheses (if any) at the start of each of the RewriteRule and RewriteCond patterns -- at least in the first and last rules. This will further reduce the number of times this ruleset will need to be processed.
Alternately, if the rule only needs to be applied to URLs that are one subdirectory level down (as in "/New_York/"), then you could include a sub-pattern of "[^/]+/" within the parentheses (if any) at the start of each pattern -- again, at least in the first and last rule.
Jim