Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite query string

tried all suggestion with no result

         

stever_nl

7:30 am on Jun 9, 2004 (gmt 0)

10+ Year Member



Hi all,

I did read all previous posts on this subject but somehow I could not get it working for me, so any help would be appreciated.

What I want to do:

I want to create search engine friendly urls from dynamicly generated php pages.

Example:
[example.com...]

What I want to transform it in is:
[example.com...]

I will have to work with a .htaccess file
the domainname is hosted on a shared hosting plan (multiple domains on 1 IP)

Hope this info covers everything for you guys to give suggestions. As I am a total newbie, please post the complete code I should use.

Thanks in advance!

Steve

[edited by: Brett_Tabke at 12:42 pm (utc) on June 10, 2004]
[edit reason] examplified. [/edit]

stever_nl

8:00 am on Jun 9, 2004 (gmt 0)

10+ Year Member



ow, yeah, this code is what I had made so far... not working, but well, that's why I'm asking:)

Options +FollowSymLinks
RewriteEngine on
RewriteBase /shop
RewriteRule product/(\.*)/(\.*)/(\.*)/(\.*)/(\.*)/(\.*)/$ /shop/product\.php?$1=$2&$3=$4&$5=$6

gergoe

10:49 am on Jun 9, 2004 (gmt 0)

10+ Year Member



...and how about the id parameter, you don't want to have it included in the new layout?

stever_nl

2:02 pm on Jun 9, 2004 (gmt 0)

10+ Year Member


Yes, ofcourse you're right, the new URL should be

http://www.example.com/shop/xbox/200/halo

thanks for pointing it out... now the solution :)

[1][[b]edited by[/b]: Brett_Tabke at 12:42 pm (utc) on June 10, 2004][/1]

dcrombie

3:49 pm on Jun 9, 2004 (gmt 0)



Your regular expression won't work because it has more parameters than the address you're going to, but if it did you would get something like:

[example.com...]

=>

[example.com...]

You really need to go back to the drawing board and work out how many variables you have, what order and what they're called...

[edited by: Brett_Tabke at 12:42 pm (utc) on June 10, 2004]

gergoe

4:06 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



stever_nl,

The Regular Expression you used will not work mainly because of the "\.*" expression, which matches zero or more occurrence of the . (dot) character. You need to use .* to achieve what you want (match zero or more occurrence of any text). But because this will match anything and everything, it might not be working as it can be expected, so I changed it a bit. I suggest you to try the following:


Options +FollowSymLinks
RewriteEngine on
RewriteBase /shop
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ product.php?platform=$1&id=$2&title=$3 [L]

You need to put this into a htaccess file in the directory where the www.domain.com/shop url points to, or if you've placed it somewhere else, then you need to change some of the directives to make it work.

jdMorgan

4:12 pm on Jun 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, the (\.*) pattern will match only "any number of periods", which is not what you want. I would suggest ([^/]+) which means, "one or more characters not equal to a slash."

Ref: Regular Expressions Tutorial [etext.lib.virginia.edu]

Jim

stever_nl

4:50 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



YAHOOOO :)

Many thanks to gergoe who understood me immediatly!

and to Jim who explained the (\.*) thingy

Cheers guys! hope this will help someone else too

Steve

stever_nl

5:26 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



I found one thing that I would like to have changed,

if my game title has more words (as most of them do) then I get an ugly url afterall in the browser address bar.

like [example.com...]

with the %20 signs for the space between the title

can I change that so it gives me
[example.com...]

with the code I've got from gergoe?

Thanks!

[edited by: Brett_Tabke at 12:43 pm (utc) on June 10, 2004]

gergoe

8:31 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



This code works as long as the order of the parameters are correct, regardless of what values you use. Characters like the slash (/) can cause some problems, since that's used to separate the parameter values, so if you have a title with / in it, better to change it, or look for an other solution. But spaces, encoded spaces (%20) and hyphens are handled properly.

stever_nl

9:13 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



Hi again!

I know it does handle everything okay, I have it already active on my site, so thanks again for that.

My question was really if I could have the %20 changed into -

Just for my visual pleasure, I saw it was possible with other signs, but could not find how to do it with %20

gergoe

10:50 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



So you want to use hyphen instead of spaces in the urls, but not to handle it by script processing the request? If so then we need to add a new RewriteRule:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /shop
[b]RewriteRule ^([^/]+/[^/]+/[^/]+)-([^/]+)/?$ $1\%20$2 [N][/b]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ product.php.ihtml?platform=$1&id=$2&title=$3 [L]

The first RewriteRule is responsible for turning the hyphens into escaped spaces, it is doing it in a loop, so if you have more than one hyphens then all will be replaced. But only in the title (last) parameter, to avoid unnecessary rewriting.

stever_nl

11:18 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



Wow, impressive, but didn't do the trick :(

Still get dynasty%20wariors%20combat

I am searching also, but this is very hard to find...

thanks for your help so far!

gergoe

12:28 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



This code does not *convert* your urls it just rewrites it which means that if you have a link on one of your pages, then you need to change the spaces to hyphens within the page, and this code will replace the hyphens with spaces when someone followed that link. The first thing can not be done by Apache. If you're using static pages, then you need to change the links manually, or if you have dynamically generated content, then you can do it using the language of your choice.