Forum Moderators: phranque

Message Too Old, No Replies

Simple rewrite question

Will this work?

         

TheRookie

6:49 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



I have this in my .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteEngine On
RewriteRule ^forums.* index.php [L,NC]
RewriteRule ^post-([0-9]*).html&highlight=([a-zA-Z0-9]*) viewtopic.php?p=$1&highlight=$2 [L,NC]
RewriteRule ^post-([0-9]*).* viewtopic.php?p=$1 [L,NC]
RewriteRule ^view-poll([0-9]*)-([0-9]*)-([a-zA-Z]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&vote=viewresult [L,NC]
RewriteRule ^about([0-9]*).html&highlight=([a-zA-Z0-9]*) viewtopic.php?t=$1&highlight=$2 [L,NC]
RewriteRule ^about([0-9]*).html&view=newest viewtopic.php?t=$1&view=newest [L,NC]
RewriteRule ^about([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4 [L,NC]
RewriteRule ^about([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2 [L,NC]
RewriteRule ^about([0-9]*).* viewtopic.php?t=$1 [L,NC]
RewriteRule ^about([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5 [L,NC]
RewriteRule ^mark-forum([0-9]*).html* viewforum.php?f=$1&mark=topics [L,NC]
RewriteRule ^updates-topic([0-9]*).html* viewtopic.php?t=$1&watch=topic [L,NC]
RewriteRule ^stop-updates-topic([0-9]*).html* viewtopic.php?t=$1&unwatch=topic [L,NC]
RewriteRule ^forum-([0-9]*).html viewforum.php?f=$1 [L,NC]
RewriteRule ^forum-([0-9]*).* viewforum.php?f=$1 [L,NC]
RewriteRule ^topic-([0-9]*)-([0-9]*)-([0-9]*).* viewforum.php?f=$1&topicdays=$2&start=$3 [L,NC]
RewriteRule ^ptopic([0-9]*).* viewtopic.php?t=$1&view=previous [L,NC]
RewriteRule ^ntopic([0-9]*).* viewtopic.php?t=$1&view=next [L,NC]

If you notice, I turn the rewrite engine on twice. Should I eliminate one of the instances of that? Or will it work fine?

Thanks.

sitz

7:11 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



Aside from a most-likely-unmeasureably-small hit to your performance, no impact.

jdMorgan

7:20 pm on Jun 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I turn the rewrite engine on twice.

Why? It's rather a waste of code and time. Once on, it stays on until turned off.

Four problems in this example:


RewriteRule ^post-([0-9]*).html&highlight=([a-zA-Z0-9]*) viewtopic.php?p=$1&highlight=$2 [L,NC]

1) It is invalid to have a URL with a query string that starts with "&" rather than the required "?"
2) Regardless of point #1, you cannot access a query string in RewriteRule. See below.
3) The pattern "a-zA-Z" is redundant (and slow) when used with the [NC] (no case) flag, which makes the compare case-insensitive.
4) Literal characters appearing in regex patterns should be escaped with a preceding "\".

Query strings are not visible to RewriteRule. You must use RewriteCond %{QUERY_STRING} to access, test, and back-reference query strings.

Fixing all of the above items, the result would be something like this:


RewriteCond %{QUERY_STRING} ^highlight=([a-z0-9]*)$ [NC]
RewriteRule ^post-([0-9]*)\.html$ viewtopic.php?p=$1&highlight=%1 [NC,L]

If 'highlight' is always going to have a value, then use "[a-z0-9]+" instead of "[a-z0-9]*". Similarly, if "post-" is always followed by at least one number before ".html", then use "[0-9]+" instead of "[0-9]*". These are subtle differences which eliminate one test case that regex has to process, and therefore improve your code's performance. Basically, be as specific as possible with your patterns.

In the following two rules, the first rule is not needed, since the second rule would also match that URL as well.


RewriteRule ^forum-([0-9]*).html viewforum.php?f=$1 [L,NC]
RewriteRule ^forum-([0-9]*).* viewforum.php?f=$1 [L,NC]

After reviewing/revising your code with the above points in mind, test it to see if it works. While we are happy to help with examples, we are not set up to do code reviews or to do your homework for you here. Please see our forum charter.

Jim

TheRookie

7:27 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



Thanks a lot for the advice.

However, before I make the changes to speed up performance, I have to get this code working first.

It originally worked, then my .htaccess got wiped out while messing with my Frontpage extensions, and now when I add it back, an error results. I'll get back to you after I find the error with the whole thing.

Thanks.

jd01

3:14 am on Jun 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi TheRookie,

I am pretty sure Jim was telling you why your rules were not working as well as speeding up performance. EG The 1st two rules and the last two rules of this set are EXACTLY the same to Apache.

This rule matches any request for aboutNUMBER.html or aboutNONUMBER.html and then fails, because you should A.)Have a line ending $, unless you know you do not need it, and B.) Should begin the right side of your rule with a / in the htaccess file C.) cannot match &highlight=([a-zA-Z0-9]*) in a RewriteRule (it is stripped by Apache.)

RewriteRule ^about([0-9]*).html&highlight=([a-zA-Z0-9]*) viewtopic.php?t=$1&highlight=$2 [L,NC]

If the above rule did qualify and attempt to redirect the path it would redirect to is:

viewtopic.php?t=NUMBER&highlight=

There is only one variable stored.

Unfortunately, three of the following four rules match exactly the same file as the one noted above... because of the {QUERY_STRING} (stuff after the?) as noted in Jim's post.

RewriteRule ^about([0-9]*).html&view=newest viewtopic.php?t=$1&view=newest [L,NC]
RewriteRule ^about([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4 [L,NC]
RewriteRule ^about([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2 [L,NC]
RewriteRule ^about([0-9]*).* viewtopic.php?t=$1 [L,NC]
RewriteRule ^about([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5 [L,NC]

These following two rules also match exactly the same pattern, because .(dot) not preceded by a \ 'matches any character except a line break' and * matches the character or set immediately preceding it 0 to n number of times, meaning in the second rule .* will continue to match any single charater until the line ends.

RewriteRule ^about([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4 [L,NC]
RewriteRule ^about([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2 [L,NC]

The correct writing of the left side of this rule would be:
RewriteRule ^about([0-9]*)\.html$ // matches only numbers 0 or more times.

To match where there will always be one or more numbers:
RewriteRule ^about([0-9]+)\.html$ // matches only numbers 1 or more numbers.

To match a number if only numbers follow the word about:
RewriteRule ^about([^.]+)\.html$ // matches everything up to the .(dot) in one pass, the most effecient if you know the word about will always be followed by a number, but never a letter or another character when requested. ([^.]*) is still more efficient than matching more than one number with [0-9]*

What you are asking for is not a simple set of rules.

I highly recommend you read up on %{QUERY_STRING} and regular expression use to complete your file, they will both be necessary, not only for speed and server load, but for the actual function. There are a number of resources in the forum charter that can give you some starting points.

I hope this gives you some ideas.

Justin

Added: Wasn't sure if you got that out of Jim's post, from your response, but wanted to make sure you knew the answer to 'will this work' is NO.