Forum Moderators: phranque

Message Too Old, No Replies

removing plus signs with rewrite

I have a problem... help appreciated.

         

JimFromSGS

5:07 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



I am doing a rewrite where the site has some plus signs in some of the values. I want to replace the plus sign with a capital X. The plus is NOT a space, it's part of a shopping checkout system the site uses and it's important - so what I'm going to do is do the rewrite and then on the coding side the site will know a capital X is really a plus.

Basically I want to take an address like this:

catalog.asp?N=36+2&Ne=1

And turn it into

36X2-1.asp

I'm grabbing the N value and the NE value, separating them by dashes, replacing the + with an X and boom.

That's what I want to do - changing the plus on the coding side is off limits unfortunately... I have to figure it out rewrite wise so my hands are a little tied.

jdMorgan

5:50 pm on Feb 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a basis for discussion.

For more information, see our forum charter [webmasterworld.com].

Thanks,
Jim

[edited by: jdMorgan at 5:51 pm (utc) on Feb. 28, 2008]

JimFromSGS

6:04 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



I have absolutely no idea how to remove the plus signs at all - that's what I need help with.

jdMorgan

7:16 pm on Feb 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Derived from [webmasterworld.com...] and several other threads here:

# Rewrite catalog.asp?N=<a>+<b>&Ne=<c> to /aXb-c.asp
RewriteCond %{QUERY_STRING} ^N=([^+]+)\+([^&]+)&Ne=([^&]+)
RewriteRule ^catalog\.asp$ /%1X%2-%3.asp [L]

This is based on your request to "remove a plus sign" using mod_rewrite, and assumes that the URL-path on the right is the actually-existing filepath on your server. That is, this takes a request for a URL+query like "catalog.asp?N=36+2&Ne=1" and returns the content from the script file located at "/36X2-1.asp" on your server.

Mod_rewrite is a "input" function, not an "output" function. Mod_rewrite operates on URLs when requested from your server, either externally redirecting them to a different URL, or "mapping" them to a different internal server filepath than that to which they would normally resolve.

Mod_rewrite has no effect on the content of pages returned in response to those requests. As such, I suspect that either the following code snippet is what you need, or that mod_rewrite cannot solve your problem as-stated. Crucial to understanding this is that the URLs which appear on your pages and are visible to client browsers (and search engine robots) are the "real" URLs; Their presence on your pages defines them as real URLs. If you want to change those URLs, then you must change the script that produces the pages -- Or "wrap" that script inside another one that parses the output from the inner script and modifies that output (this can be horribly inefficient).

If, in contrast to the title of this thread and the discussion above, the "real" URL that appears on your pages is "36X2-1.asp" and you want a request for that URL delivered to the script file "/catalog.asp" with name/value pairs of "N=36+2&Ne=1", then this code does so:


# Rewrite <a>X<b>-<c>.asp to catalog.asp?N=<a>+<b>&Ne=<c>
RewriteRule ^([^X]+X([^-]+)-([^.]+)\.asp$ /catalog.asp?N=$1+$2&Ne=$3 [L]

So, there are two examples which are complementary in function -- within the limited scope of your example above.

The resources cited in our forum charter and the contents of our forum Library may be quite helpful in understanding this.

Jim

JimFromSGS

8:10 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



All hail the rewrite God. Thank you sir!