Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite, Multiple word variable in url.

         

dkin

11:39 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



I have successfully rewrote many tables on my site, I am having problems with this one because in the url I have a variable which can be between 1-4 words, depending on the page called.

This is the url now.

file.php?name=Zone_Database&file=zone_detail&zoneid=$var

which I would like to rewrite to

Zone_Database-Zone-Name-$var.html

this is my attempt to rewrite it

?name=Zone_Database&file=zone_detail&zoneid=([A-Z][a-z][a-z]*)

but that does not work.

Any suggestions on how to rewrite multie string variablse?

I beleive all I need to do is change this

([A-Z][a-z][a-z]*)

to the correct coding.

Thanks all

Cheers

jatar_k

4:41 am on Aug 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A good place to start dkin is this tutorial right here
An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]

dkin

6:05 am on Aug 13, 2004 (gmt 0)

10+ Year Member



I read that article as soon as I saw the link.

It is a great article for the basics but does not deal with variables, or multi string variables.

I am baffled with this, sorry for bein a newbie lol.

Thanks for all suggestions.

Cheers

jdMorgan

12:56 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dkin,

mod_rewrite doesn't care how many "words" are in a variable. Since your variable is delimited by by "zoneid=" on the left end, and possibly by "&" on the right, the regex to match it and put it into a back-reference would be:
zoneid=([^&]+)

Jim

dkin

8:41 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



I have tried your suggestion and all I am getting is a 500 error.

Forgive me if this sounds at all arrogant but, I am using phpnuke (from what I have heard this may be frowned upon.) Now with phpnuke I have looked through the HTACCESS and to rewrite urls it is using code such as this

RewriteRule ^Item_Database-([1-9][0-9][0-9]*).html modules.php?name=Item_Database&file=item_page&id=$1

That code, along with a duplicate in the urlin array in header.php and a urlout array which looks like this

"Item_Database-\\1.html"

Changes a url such as this

modules.php?name=Item_Database&file=item_page&id=296

into this

"Item_Database-296.html"

lol Do not ask me how this works, I do not know. But it does work, I have almost my entire site rewritten but, for some reason these multiple word variables come oout different.

If I want something like this to be the output

"Zone_Database-Zone-Name-$var.html"

It works fine for 1 word variables but more than one word is output like this

"Zone_Database-Zone-Name-$word1.html$word2$word3$word4"

Which obviously does not work.

If I am just rambling and none of this makes sense I am sorry but this is my problem and I am hoping that someone knows what I can do.

Thank you very much.

Dylan Kingsberry

Cheers.

jdMorgan

9:57 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dylan,

RewriteRule ^Item_Database-([1-9][0-9][0-9]*).html modules.php?name=Item_Database&file=item_page&id=$1

can be recoded more efficiently and correctly as

RewriteRule ^Item_Database-([1-9][0-9]{1,})\.html$ modules.php?name=Item_Database&file=item_page&id=$1

so I don't think you're dealing with mod_rewrite experts, here.

This code actually changes a url such as "Item_Database-296.html" to "modules.php?name=Item_Database&file=item_page&id=296", so knowing the "direction" of the rewrite may help you to understand the process. The true URLs seen by users and search robots are static, and those are rewritten to dynamic form to call your script. WebmasterWorld uses a similar approach.

It sounds like there's probably some code interaction going on that is messing things up.

There are links to mod_rewrite and regular expressions resources in our Apache Forum charter that may be of use to you.

Jim

dkin

10:36 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



If I want something like this to be the output

"Zone_Database-Zone-Name-$var.html"

It works fine for 1 word variables but more than one word is output like this

"Zone_Database-Zone-Name-$word1.html$word2$word3$word4"

Do you have any suggestions for this?

jdMorgan

10:42 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, find out why your script is doing this. Mod_rewrite does not care about word boundaries, as stated in my first post. In fact, making mod_rewrite detect word boundaries is a pain, because there is no built-in word-boundary parsing support as there is in scripting languages.

Jim

dkin

11:55 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



I have no idea why my script is doing this. I have tried everything I know and this is the best that I can get.

with single words if I had this [A-Z] it would only put the first letter. So like this.

S.htmlite

when I added this [A-Za-z]

I would get this.

Site.html

which is right, is there something equivalent to this for words.

I am very sorry but I have no idea what i am doing and this forum is my only chance for getting this resolved.

Thank you

Cheers

gergoe

1:26 am on Aug 14, 2004 (gmt 0)

10+ Year Member



The first point of the failure is here:
I am very sorry but I have no idea what i am doing

* Explain your goals as much as possible; what kind of links do you have on your pages (with some examples) and what kind (number, occurrences/dependency) of input variables are expected by your script driven pages.
* Be helpful; if something is not working, try to spot the problem, take a look in the logfiles of the webserver, try to simplify the used mod_rewrite directives, or add some static test patterns.
* Be creative; take a look in the mod_rewrite documentation [httpd.apache.org] for the syntax and some examples, the URL Rewriting Guide [httpd.apache.org] for practical solutions, and try to put things together. You might feel all this just too difficult and useless, but driving a car might be also difficult, but still a lot of people do it. Nothing is impossible

By the way in think you are on the wrong track (or you just misled me too)
mod_rewrite processes the incoming urls only; so only processes the requests sent to the webserver. For example if I want to do what you're asking for (I think ;-) then I'd do something like this in my htaccess file:


RewriteEngine on
RewriteRule ^Zone_Database-Zone-Name-(.+)\.html?$ file.php?name=Zone_Database&file=zone_detail&zoneid=$1 [L]

This will take care that if I go to the
http://www.domain.com/Zone_Database-Zone-Name-Anything goes here.html
url, it will be internally interpreted as
http://www.domain.com/file.php?name=Zone_Database&file=zone_detail&zoneid=Anything goes here
. This is only for the incoming requests.

But, if you have links like this

http://www.domain.com/file.php?name=Zone_Database&file=zone_detail&zoneid=Anything goes here

on your site then mod_rewrite can not to nothing with these links, it will be sent to the client as-is, since the content of the pages aren't processed by mod_rewrite, and there's no way to do so. If you want to change your site from "dynamic" urls to "static" ones, then you need to change it yourself in the html files and in your scripts. Then with mod_rewrite you can take care that the new "static" links will be processed properly.

Hope this helps

dkin

4:01 am on Aug 14, 2004 (gmt 0)

10+ Year Member



So far I have taken a suggestion from JD and placed this in my HTACCESS, ([A-Z][A-Za-z-]+)

Now is I have to strings

whistle-1
whistle 1

Like so this will output

whistle-1.html
whistle.html1

So now I need it to take spaces as well, so if the word seperator is either a - or a space it will take it.

I will look over the forum charter but, please if you know please post.

Thank you

Cheers.

jdMorgan

2:01 pm on Aug 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just add "\ " (backslash space) to your list of acceptable characters, ([A-Za-z][A-Za-z\ -]+)

If those spaces are %-encoded (check your server log to see), then things get a bit more complex. In that case, you'll need ([A-Za-z]([A-Za-z-]¦\%20)+)

Note that in the pattern above, you'll have to edit the "¦" character, and change it to a solid vertical pipe -- posting pipes on this board changes them for some reason, and you have to edit them before use.

Jim

dkin

4:06 pm on Aug 14, 2004 (gmt 0)

10+ Year Member



That did it, Thank you so much Jd.

Nothing like tips from the pros :D