Forum Moderators: phranque

Message Too Old, No Replies

.htaccess almost working right.

How do I convert "+" character in URLs?

         

seminole

4:33 pm on Apr 22, 2006 (gmt 0)

10+ Year Member



HELP, here's what I have below, I'm trying to stop using the + in the URL. If I have multiple search terms, I want to use a "-" as a delimiter, not the "+" that I must use now. I get an error if I use a dash rather than a plus right now. I guess I need to turn that - into a +, but I can't find the right sysntax...

NOW, MY SE FRIENDLY URL IS
mydomain.com/folder/term+term.html
BUT, I WANT TO USE
mydomain.com/folder/term-term.html

RewriteEngine on

RewriteRule ^folder/([a-zA-Z0-9\-\+\&\%\ ]+)\.html$ folder/search.php?searchterm=$1
RewriteRule ^folder/([a-zA-Z0-9\-\+\&\%\ ]+)/([a-zA-Z0-9\-\+\&\%\ ]+)/$ folder/search.php?offset=$1&searchterm=$2
RewriteRule ^folder/([a-zA-Z0-9\-\+\&\%\ ]+)/([a-zA-Z0-9\-\+\&\%\ ]+)/([a-zA-Z0-9\-\+\&\%\ ]+)$ folder/search.php?offset=$1&searchterm=$2&language=$3

jdMorgan

2:39 am on Apr 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use PHP's preg_replace function to change the "+" characters in the links on your site to "-" characters instead.

Then use mod_rewrite to change those URLs back, if necessary, to call your script.

More info here [webmasterworld.com].

Jim

seminole

12:20 pm on Apr 23, 2006 (gmt 0)

10+ Year Member



Thank you for the help. But, Are you saying that this mod rewrite can't accomodate what I'm needing to do? Isn't it just changing a few characters in this .htaccess?

Maybe you misunderstand, or I'm miscommunicating. Don't worry about my links in pages etc. It's a new site, new content. So, as I create it, I want the mod rewrite to handle a link like this.

Current: MySite/MyFolder/term+term.html = works fine now

Desired: MySite/MyFolder/term-term.html = want this to work

Can someone look at the code and tell me how I can do this with the mod rewrite? Or, confirm it isn't possible, I prefer to fix this in Mod rewrite if at all possible. I am a total beginner with mod rewite, I barely know where it goes and what it can do. It may be simple (i think) but I need help with this "+"

many thanks in advance.

jdMorgan

3:59 am on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The link on your page that the visitor sees in his browser defines the URL, and mod_rewrite cannot change that URL. It can only change the *file* associated with that URL, when someone requests that URL from your server by clicking on the link on the page. (It may help to see a server as a URL-to-filesystem translator, which is a large part of what it is).

Based on those facts, what is the URL published on your page, and how do you want to change the file association of that URL?

I see rules that map URLs like mydomain.com/folder/term/term.html to the file /folder/search.php?offset=$1&searchterm=$2

where mydomain.com/folder/term/term.html is the URL and /folder/search.php?offset=$1&searchterm=$2 is the associated file-plus-query-string.

I do not see any rules that map URLs with "+" or "-" as parameter *delimiters*, and maybe that's where I'm falling off this train. If the example needs adjustment to be consistent with the discussion, please do adjust it.

BTW, you can write ([a-zA-Z0-9\-\+\&\%\ ]+) as ([a-z0-9&+\-\%\ ]+) by using the [NC] flag on RewriteRule:
[NC] makes the compare case-insensitive, saving some processor time, and not all of those characters need to be escaped within a regex group.

Jim

seminole

1:10 am on Apr 26, 2006 (gmt 0)

10+ Year Member



>> It can only change the *file* associated with that
>> URL, when someone requests that URL from your server
>> by clicking on the link on the page. (It may help to
>> see a server as a URL-to-filesystem translator,
>> which is a large part of what it is).

Yes, Great explanation. I'm with you. The SE friendly URLs I'm talking about will be what I manually use building pages or typing in browsers. It all works exactly as I like, EXCEPT where I must pass a multi-word search term. I am waiting to build any links till I get them working exactly like I want. If I use a multi-word search term, I want to pass that in my browser with a "-" not a "+".

>> I see rules that map URLs like
>> mydomain.com/folder/term/term.html to the file
>> /folder/search.php?offset=$1&searchterm=$2

Well Yes, very close it maps: "mydomain.com/folder/big+hooters.html"
<not>
"mydomain.com/folder/big/hooters.html"

Its just that where I map that variable "searchterm", in a case like above where is it a multi-word search term, like "big+hooters" I want to use the link in my browser URL:

mydomain.com/folder/big-hooters.html

Currently, If it's a single word search term, like "hooters" it currently works if I use:

mydomain.com/folder/hooters.html

But, currently if I use a multi-word search term in the URL, I MUST supply the link like this to the browser, with a "+"

mydomain.com/folder/big+hooters.html

But I Want: mydomain.com/folder/big-hooters.html

My PHP search scripting wants that as a "+" when it processes that multi-word search term, not a "-". I am trying to modify my .htaccess to handle that.

>> BTW, you can write ([a-zA-Z0-9\-\+\&\%\ ]+) as
>> ([a-z0-9&+\-\%\ ]+) by using the [NC] flag on
>> RewriteRule:

>> [NC] makes the compare case-insensitive, saving some
>> processor time, and not all of those characters need
>> to be escaped within a regex group.

Damn, I wish i understood that better. I'm going to try to figure that out. I think I understand what you are saying but will have to figure out what to do to employ it. Jim, thank you so much for your time. I am trying to communicate and learn this stuff and I appreciate you taking the time to help. I think I'm learning a little.

fjpapaleo

1:50 am on Apr 26, 2006 (gmt 0)

10+ Year Member



It sounds like you're URL encoding your string. Where you actually have "test test" in your database it gets URL encoded as test+test. What JD is trying to tell you is you must have test-test sent to the browser BEFORE you do the rewrite.I have a similar issue and solved it by using an ereg replace in place of the urlencode. Something like:
ereg_replace(" ","-",stripslashes($data])).

That should give you test-test. Now the problem I'm having is with my rewrite getting screwed up because of the multiple dashes in the URL. :(