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. :)
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]
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
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
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...
perhaps I should just put these changes into the site root?
> 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
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
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 :)
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.