4 replies [Last post]
velo
velo's picture
Offline
Enthusiast
Fayettenam
Last seen: 13 years 15 weeks ago
Fayettenam
Timezone: GMT-5
Joined: 2004-11-11
Posts: 181
Points: 0

I have mod_rewrite installed and it's working. I just need to figure out the regular expression syntax for the .htaccess file.

This is what is happening. The following rewrite rule IS working-

RewriteRule (.*)-(.*)(\.htm)$ /?$1=$2

this will convert "%domain%/content-contact.htm into %domain%/?content=contact which is the dynamic URL that my site needs.

As I understand it the follwing bit within quotes means that any number of characters of any type are matched until a hyphen is rached "(.*)-" and it seems to be working fine. But when I try to extend this to a second rule for a second set of variable the whole thing breaks down. The problem is that I need to keep the static URLs in the top level directory or the whole thing breaks down.

Here is my .htaccess file-

Options +FollowSymLinks
RewriteEngine on
rewriteBase /
RewriteRule (.*)-(.*)(\.htm)$ /?$1=$2
RewriteRule (.*)-(.*)-(.*)-(.*)(\.htm)$ /?$1=$2&$3=$4

So why does the first rule and the second doesn't? I realize this is probably due to my ignorance and so please also include any scathing insults you may have for me in your reply.

Cheers!

Give a man a fire and he will be warm for a while. Set a man on fire and he will be warm for the rest of his life.

Tags:
Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 9 years 41 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Re: Way off topic......Apache mod_rewrite module

If a rule matches and that request doesn't need to progress through further rules you should use the [L] flag.

If you are adding a query string you should use [QSA] flag.

Personally I would dump the whole of the string into the query string as one value (e.g. ?data=<all the incoming data>) and then process it inside your script. That makes it much easier to handle multiple variables. You could send the revised URL back into Apache at the beginning again but its not really and ideal use for Apache.

RewriteRule ^(.*)\.htm$ index.php?data=$1 [L,QSA]

Then first thing in your script retrieve $_GET['data'] and process it into whatever you need, e.g. in php ...

$data = explode('-',$_GET['data']);
for ($i=0; $i < count($data); $i+=2) {
  if (isset($data[$i+1]))  $_GET[$data[$i]] = $data[$i+1];
}

now you have parsed all your incoming data into the form expected carry on with the rest of the script.

If you really don't want to add to your existing script - you can construct a new query string and issue a redirect http request.

e.g.

header("Location: ".$mynewquerystring);
die;

just make sure your rewrite rule realises there are two places it can send incoming request - ones from the user to your script and the redirect from your script to the correct script.

I hope all of the above makes sense Smile

velo
velo's picture
Offline
Enthusiast
Fayettenam
Last seen: 13 years 15 weeks ago
Fayettenam
Timezone: GMT-5
Joined: 2004-11-11
Posts: 181
Points: 0

erm.....

interesting idea, I didn't think of approaching it that way. It would just be a matter of associating the proper variable name (to mesh with the existing scripts) and the proper value.

Thanks for the excellent suggestion, I may try implementing this if I can't find a usable rule for the .htaccess.

I also would like to figure out mod_rewrite a little better so I am going to keep on looking for why this rule doesn't work. Besides, it would still be the easiest thing so I don't have to write scripts for three different sites! Wink

CHEERS!

Give a man a fire and he will be warm for a while. Set a man on fire and he will be warm for the rest of his life.

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 9 years 41 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Way off topic......Apache mod_rewrite module

(1) My idea is ubiquitous - you only need to write one script, it adds the variables into the $_GET global (you can also add them to $_REQUEST). So the script is variable independent. The redirect even allows you to use one script to call more than one of the original scripts (though you could do that through includes too) and so it does what you are trying to achieve with htacceess and mod_rewrite.

(2) Think about your two patterns and the order. Remember apache doesn't know anything about the second pattern when it reaches the first. And also remember that "." matches any character...

velo
velo's picture
Offline
Enthusiast
Fayettenam
Last seen: 13 years 15 weeks ago
Fayettenam
Timezone: GMT-5
Joined: 2004-11-11
Posts: 181
Points: 0

Apache and rule order

Right. I got it while playing with it last night. It actually works if I take the second rule, limit the scope of the matched characters, expand the rule to allow for all 4 variable/value pairs, and use that as the only rule! RewriteRule ([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)-([a-zA-Z0-9]*)(\.htm)$ /?$1=$2&$3=$4&$5=$6&$7=$*

Weird, I would expect that to crash because when using one variable/value pair the rest of the expression on both sides is unused. mod_rewrite really is voodoo....

Now I am going to try that script.......

THANKS!!!!

Give a man a fire and he will be warm for a while. Set a man on fire and he will be warm for the rest of his life.