Web Hosting Talk







View Full Version : Mod_rewrite help, can it be done?


mrsam
06-12-2005, 06:57 AM
Can the following be done with the use of Mod_rewrite?

I have the following URLs (with $variables):
domain.com/view.php?system=$row[system]&type=$row[type]&shortname$row[shortname]

example (without $variables);
domain.com/view.php?system=PC&type=Help&shortname=titlenamehere

With the help of Mod_rewrite I would like to make it look like this;
domain.com/PCHelp/titlenamehereHelp.htm

or

domain.com/PC-Help/titlenamehere-Help.htm

Is this possible?

error404
06-12-2005, 07:13 AM
The second option should be quite simple, provided none of your possible system values has a dash in it.

mrsam
06-12-2005, 12:52 PM
Some people seems to have misunderstood my post, so i'll just try and explain it better;

have to use a .htaccess file ..

We need to be aware of changing variables.

This little piece of rewriting code partly does what im looking for, but not just quite (only to illustrate)

---
RewriteEngine On

RewriteRule ^([^./]+)/([^./]+)/([^./]+).htm$ view.php?system=$1&type=$2&shortname=$3 [L]
---

This would turn an original URL like;
domain.com/view.php?system=$system&type=$type&shortname=$shortname

into;
domain.com/$system/$type/$shortname.htm

However I want it turned into:
domain.com/$system$type/$shortname$type.htm

or

domain.com/$system-$type/$shortname-$type.htm

or atlast - without $type after $shortname at the end

Burhan
06-12-2005, 01:29 PM
Err, mod_rewrite works the opposite way.

It rewrites The URL typed in the browser, to whatever you specify.

So your wording is not correct, but I hope your understanding of mod_rewrite isn't.

So what you really want mod_rewrite to do is take a URL such as domain.com/$system$type/$shortname$type.htm and rewrite it to view.php?system=$system&type=$type&shortname=$shortname

For this to happen, you need to write a regular expression that can distinguish between $system and $type -- this is why error404 said that the second option would be easier, because you can have the regular expression separate between the - in the $system-$key pattern.

mrsam
06-12-2005, 01:37 PM
Originally posted by fyrestrtr


So what you really want mod_rewrite to do is take a URL such as domain.com/$system$type/$shortname$type.htm and rewrite it to view.php?system=$system&type=$type&shortname=$shortname


Nope, it's actually the other way around.

I want to take a URL such as;
domain.com/view.php?system=$system&type=$type&shortname=$shortname

and rewrite it to
domain.com/$system$type/$shortname$type.htm
or
domain.com/$system-$type/$shortname-$type.htm

Sorry if i've expressed myself wrongly, my mistake :/

Burhan
06-12-2005, 02:00 PM
So what do you want the user to type in the browser? the view.php?system= or the /$system-$type/ ?

mrsam
06-12-2005, 02:18 PM
I want the user to type in;

domain.com/$system$type/$shortname$type.htm
or
domain.com/$system-$type/$shortname-$type.htm

Burhan
06-13-2005, 02:45 AM
This is what I thought. You then want to rewrite the domain.com/$system-$type/$shortname-$type.htm into view.php?

For this,

RewriteRule ^(.*?)-(.*?)/(.*?)-(.*?).htm$ view.php?system=$1&type=$2&shortname=$3&stype=$4.php

Should work, although its early.

shintainuk
06-13-2005, 07:19 AM
I heard theres a good site called doriat.com discussing about mod rewrite on php scripts.

mrsam
06-14-2005, 04:13 PM
fyrestrtr > got it working, thanks alot :)