Forum Moderators: phranque

Message Too Old, No Replies

Fast Problem Easy Fix. Rewrite Condition

Htaccess rewrite , conditions etc.

         

acimag

3:01 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hello, Ive gotten help here before and I try to pitch in whenever its not over my head. But today Im doing SEO on a client an I need a rewrite condition written. But I can't figure it out.

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]

jdMorgan

3:29 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a fast fix, because mod_rewrite has no recursion capability, and you'll have to replace "-" with "--" on a character-by-character basis. Doing multiple sequential rewrites also triggers a bug in mod_rewrite, requiring special techniques to work around the problem.

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

acimag

4:00 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



You know what I did. A line by line 304 rewrite. took some time. But It's done. I thought it was 1000's of rewrites. luckily google didnt index everything so it was only like 100. So its still done. thanks for the help though JP I appreciate it.

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.

jdMorgan

5:30 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A line-by-line "304" rewrite does not sound correct...

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]

Here, we use the variable "myURL" to work on the URL, and the requested URL seen by RewriteRule is not updated until the very last step so as to avoid the Apache bug. This leads to some inefficiency, which is only partially compensated by using the chaining function to avoid executing some of the rules when they're not needed.

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