Forum Moderators: phranque

Message Too Old, No Replies

.htaccess fro On-the-fly Content-Regeneration

I would like to take xx.html and rewrite it to a query string.

         

vrtlw

8:54 am on Feb 23, 2004 (gmt 0)

10+ Year Member



I have succesfully implemented a .htaccess rule in a test environment that I would like to go live with.

The only problem with it is that I have to setup RewriteRule for every xx.html page that I want to rewrite. I would prefer to be able to take the xx value and make the RewriteRule add it as a query string, for example:

User goes to http*//www.mydomain.com/articles/category/3.html

Server serves up http*//www.mydomain.com/cgi-bin/articles/morearticles.cgi?category=3

This works fine with the following .htaccess

code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!s
RewriteRule ^3\.html ../../cgi-bin/articles/morearticles.cgi?category=3 [T=application/x-httpd-cgi,L]

Any idea how I can take the 3.html and send it to the query string. I want to have a lot of categories an would rather not write a rule for each category number.

p.s. There is a space between } and!, I just couldnt work out how to get it to appear in my post.

vrtlw

9:34 am on Feb 23, 2004 (gmt 0)

10+ Year Member



Doh,

I got it, read back a few threads and found (.+)

vrtlw

12:58 am on Feb 24, 2004 (gmt 0)

10+ Year Member



Ok, this works fine until I start trying to work with a URL similar to the following:

http*//www.mydomain.com/cgi-bin/articles/morearticles.cgi?category=3&page=2

My thought was to use a format something similar to this:

http*//www.mydomain.com/articles/category/3-2.html

I cannot however find a way to get both parts of the URL passed to the query string.

Here is the .htaccess rewrite rule that works when there is only one value in the query string:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-s
RewriteRule ^(.+)\.html ../../cgi-bin/articles/morearticles.cgi?category=$1 [T=application/x-httpd-cgi,L]

TIA

Note, there is a space in the condition after the }

vrtlw

2:19 am on Feb 24, 2004 (gmt 0)

10+ Year Member



Ok, what law do they call this?

Within an hour of posting this I get it to work :)

I got frustrated but here is the code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-s
RewriteRule ([0-9]+)-([0-9]+)\.html$ ../../cgi-bin/article/morearticles.cgi?category=$1&page=$2 [T=application/x-httpd-cgi,L]
RewriteRule ^(.+)\.html ../../cgi-bin/articles/morearticles.cgi?category=$1 [T=application/x-httpd-cgi,L]

jdMorgan

2:31 am on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you need to do is to create two back-references, which you do by enclosing the sub-patterns to be matched in parentheses.

To parse out the "3-2" from 3-2\.html, you'd use something like:

 ([0-9])-([0-9])\.html$ 

That works for two single digits, 0 through 9, separated by a hyphen.

You then back-reference the first digit with $1 and the second with $2 in the substitution URL.

Need more digits? You can specify an upper and lower bound using something like this:

 ([0-9]{1,4})-([0-9]{1,2})\.html$ 

That accepts a one-to-four-digit number as $1, and a one-or-two-digit number as $2.

Another way to do this, for example if the parameters are not exclusively numeric, is to accept anything until you find a hyphen and put that into $1, and then accept anything after the hyphen until you find a period and put that into $2:

 ([^-]*)-([^.]*)\.html$ 

Ref: [etext.lib.virginia.edu...]

Jim

vrtlw

3:24 am on Feb 24, 2004 (gmt 0)

10+ Year Member



Thanks Jim,

You had the foresight to see my next issue.

I now have categories < 99999 and pages < 999

Thanks again