Forum Moderators: coopster & phranque

Message Too Old, No Replies

playing with apache mod_rewrite for the 1st time

         

jeremy goodrich

7:46 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to get a url fixed for search engines that goes like this:

site-i'm-workign-on.com/stuff/dynamic.html?var=XYZ&0

i'd like it to look this way:

site-i'm-workign-on.com/stuff/dynamic/var/XYZ/0

this is the rewrite stuff I have so far:

RewriteEngine on
RewriteBase /stuff/
RewriteRule ^/(.*).html(.+?)=(.+?)\&(.*)$/$2/$3

does that make any sense? don't want to kill a live site, so I'd appreciate any help/comments, wise cracks. scratch that, I just want help. :)

gethan

8:17 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was doing just this for the first time a few weeks ago - its now working a treat.

It seems to me in your example you've got it twisted the wrong way - what your doing is directing a dynamic url request to static content that won't exist...

I think your example should be rewritten as

[perl]
RewriteBase /Stuff/
RewriteRule ^dynamic/(.*)/(.*)/$ /dynamic.html?$1=$2&$3 [T=application/x-httpd-cgi,L]
[/perl]

The way I tested this was to make a directory Stuff and make this the .htaccess file. I then just typed in the urls and looked at the content.

The next stage is to replace all the links in your sites with the /Stuff/var/xyz/0 style links and voila.

I seriously recommend making a test sub-domain and cloning your site for this.

Also - the regex's used in mod_rewrite are more limited than perl - non-greedy as standard. I don't think + is supported either. So basically just use (.*) and literals.

Good luck

Ps.

This is a snippet from a live site :) (sort of - to show theory in practice)

[perl]
RewriteEngine on
RewriteBase /Genres/
RewriteRule ^(.*)-([^-]*)\.html$ /viewbook.php4?title=$1&id=$2 [T=application/x-httpd-cgi,L]
RewriteRule ^(.*)/$ /viewgenre.php4?genre=$1 [T=application/x-httpd-cgi,L]
RewriteRule ^$ /genres.php4 [T=application/x-httpd-cgi,L]
[/perl]

amoore

8:34 pm on Dec 5, 2001 (gmt 0)

10+ Year Member



I think there are a couple of problems there, but everyone sees this voodoo in a different way
1) aren't the two expressions supposed to be seperated by whitespace instead of a "/" as they are in real regular expressions? (you may have it right, and the textentry thing on this website ate it.)
2) You have to match that "?" so that it doesn't show up in your $2, don't you?
3) you have to put "$0" in the second expression so that the "dynamic" shows up and "$1" so that the "var" shows up, I think
I would go for something that looks more like:

RewriteRule ^/(.*).html\?(.*)=(.*)\&(.*)$ /$0/$1/$2/$3

I'm not sure what you were going for with the "(.*+)" things, so I made them "(.*)" because that should match. You may want "(.+)" though.
Also, I believe this will break down if you have more than one name/value pair in your URL, but I wouldn't know what to do with that anyway.
Hope it helps. It should at least get you closer than you were. More than that, I hope you have a way to test this out before you put it in place! Good luck

seriesint

8:59 pm on Dec 5, 2001 (gmt 0)



Hi,

I got a question or two about the rules ya'll used. I must be missing something in the mod_rewrite docs, since both of the examples for the orginal question the last match is left off?

RewriteRule ^/(.*).html(.+?)=(.+?)\&(.*)$/$2/$3
$1 would match the file name
$2 would match ?var or var (not sure how the ? is missed)
$3 would match XYZ
$4 would match 0

That assumes gethan meant the ? for controlling greedy nature isn't supported by mod_rewrite else I'm totally lost since the docs say that + is 1 or more just like perl.

RewriteRule ^dynamic/(.*)/(.*)/$ /dynamic.html?$1=$2&$3 [T=application/x-httpd-cgi,L]

Which makes me wonder where $3 gets is value from. Does mod_rewrite have a basket catch for everything else? And just as a question from the blue but does that last style of Rewrite Rule require escaping? I remember reading about that at some point.

Mind you, I'm asking from a perl regex point of view. My mod_rewrite exp is "RTFM". I got more questions but I'll just go install apache to see what's up.

later

jeremy goodrich

9:06 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, here is the current state of my .htaccess file for the app in question:

Options ExecCGI
AddType application/x-httpd-php .php
AddHandler php-script .htm

RewriteEngine on
RewriteBase /stuff/
RewriteRule ^scriptname/(.*)/(.*)/$ /scriptname.htm?$1=$2&$3 [T=application/x-httpd-php,L]

I'm still waiting for my ftp prog to get this thing up, so I can see if it fries or not. Crossing fingers...

jeremy goodrich

9:47 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this is wacky, I'm getting a 403 error from the directory now :( Any ideas why that would happen?

littleman

9:59 pm on Dec 5, 2001 (gmt 0)



Permission denied. Check your apache config file (httpd.conf) to see if it allows overwrites.

jeremy goodrich

10:12 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've customized the .htaccess file at the root level for the site, but this subdirectory isn't working out right. Perhaps that means I can only use one custom .htaccess file? there is another one in another directory, that ones working out fine...

perhaps I should just put these changes into the site root?

littleman

10:21 pm on Dec 5, 2001 (gmt 0)



Do you have server access? It could be that apache is set up to only allow rewrite in the root folder, but not subs. If you do have server access take a look at the httpd.conf file's allow overwrite setup.

jeremy goodrich

10:46 pm on Dec 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't have server access, but I can contact the host. I'll ask them about that...in the mean time, is the stuff right on that .htaccess file I posted?

gethan

10:14 am on Dec 6, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jeremy;

> RewriteRule ^scriptname/(.*)/(.*)/$ /scriptname.htm?$1=$2&$3 [T=application/x-httpd-php,L]

[edit just noticed that I missed off the 3rd .* in my original post - sorry]

I think you should try

RewriteRule ^scriptname/(.*)/(.*)/(.*)$ /scriptname.htm?$1=$2&$3 [T=application/x-httpd-cgi,L]

Assuming you're after a mapping for

/script/var1/val1/val2 -> /script.html?var1=val1&var2

I'm not sure why you get a 403 - could be many related things...

>That assumes gethan meant the ? for controlling greedy nature isn't supported by mod_rewrite else I'm totally lost since the docs say that + is 1 or more just like perl.

I was a bit rushed posting the reply - But yep ... when I first tried out mod_rewrite I assumed the regex's were just like perl and stopped reading the docs ;) ... playing with it further I found that my tests really only worked with .* - and the ? isn't supported - it seemed to treat the ? as a literal.

If the docs say the + does work - then it will - I'll have to start using it :)

As for non-greedy - thinking about it further - I don't think I did enough testing to confirm this... it probably is as you say just non-greedy matching isn't supported.

Apologies for making this difficult subject more confusing

seriesint

4:38 pm on Dec 6, 2001 (gmt 0)



Think half the voodoo of mod_rewrite is how confusing it is to discuss ;)

I looked over the doc again and its ? is 0 or 1, rest fall into line. And a search for "greedy" got no results. So either they use another term or its not even an option. Umm come to think of it greedy matching should be limited by the use of ( ) no? Think so but anyway thanks for clearing up those parts.

Soon as I get my dev server back up, I'll come back with some questions of my own ;)

oh ya, how does mod_rewrite handle error messages ? Shouldn't there be some indication of what's going on with his rule in the log files?
I halfway thought that with the original ruleset, where it was converting to a directory style with the last match missing , the url would end with a / and with directory indexes off that would give an error. Not sure if thats any help but the log files should have something.

later

jeremy goodrich

6:25 pm on Dec 6, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wacky stuff, my dynamic stuff is parsing just fine as htm, but the rewrite just isn't doing anything (finally made sure it's compiled in to apache with my hosting provider)

Here is the current 'voodoo' :
RewriteEngine on
RewriteBase /stuff/
RewriteRule ^index/(.*)/(.*)$ /index.htm?$1=$2 [T=application/x-httpd-php,L]
RewriteRule ^viewstuff/(.*)/(.*)/(.*)$ /viewstuff.htm?$1=$2&$3 [T=application/x-httpd-php,L]

Crazy thing, I'm getting 404's when I type in x/y/z but the dynamic url is working fine. Any ideas? Thanks for all the help, this is why I love this place :)

gethan

9:48 pm on Dec 6, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've set up a test - www.ewonder.co.uk/viewstuff.php4

and www.ewonder.co.uk/viewstuff/var1/val1/var2

my .htaccess is www.ewonder.co.uk/viewstuff/htaccess

RewriteEngine on
RewriteBase /viewstuff/
RewriteRule ^(.*)/(.*)/(.*)$ /viewstuff.php4?$1=$2&$3 [T=application/x-httpd-php,L]

And it works - the 3rd var dosen't have a val so it dosen't show in my test script but the request uri is shown.

I tested it both with and without the existance of viewstuff dir, with the .htaccess in the root it meant that everything /a/b/c/d was passed to the script. In the specially created dir it only worked for that directory.

So it's not your rewrite syntax - but something else. Try it with a file extension of .php without the addhandler... and then I'm out of ideas... :(

Hopefully your webhosts will take it seriously and look at it.

jeremy goodrich

10:09 pm on Dec 6, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for helping somebody who's a little slow these last couple days...