Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rules efficiency

tactics for regex use in rewrite rules; redirect

         

jasimon9

2:59 pm on Feb 8, 2016 (gmt 0)

10+ Year Member



I have some rewrite rules of the following form:

RewriteRule ^one\.php$ /new.php [L,NC,R=301]
RewriteRule ^two\.php$ /new.php [L,NC,R=301]
RewriteRule ^three\.php$ /new.php [L,NC,R=301]

1. I am wondering if a regex to combine the three rules into one would be an improvement, efficiency-wise.

2. I am wondering if a Redirect would be better. It is important to have the 301 redirect for SEO purposes.

whitespace

4:06 pm on Feb 8, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



1. I am wondering if a regex to combine the three rules into one would be an improvement, efficiency-wise.


Well, possibly. It would certainly take up less bytes and might even be easier to read, so for those reasons I would combine these rules. (However, if you are accessing "/one.php" 99% of the time then maybe having separate rules would be preferable?!)

Only use the NC flag if you specifically want/need a case-insensitive match.

2. I am wondering if a Redirect would be better.


Ah, the Redirect (mod_alias) directive, as opposed to RewriteRule (mod_rewrite) ... that depends on whether you are already using mod_rewrite for other redirects/rewrites? If not, then yes, a Redirect (or RedirectMatch if you wish to combine these directives) is preferable.

In the interests of "efficiency" you would create these redirects in your server config and disable .htaccess processing.

lucy24

4:36 pm on Feb 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is important to have the 301 redirect for SEO purposes.

Well, also for human purposes. You want people to go to the intended URL, don't you?

No matter what else you do, make sure the target includes the full protocol-plus-domain, like http://www.example.com/etcetera. Otherwise the server will reappend whatever the request came in with, which might happen to be wrong. (Perhaps intentionally wrong, in the case of search engines. Google appears to be on a housekeeping kick just now.)

Is it your own server or shared hosting?

I hope you don't have a vast number of files all redirecting to the same URL. Three is fine, but if there are lots and lots of them, it might be better to serve a 410 and then make a nice custom 410 page.

jasimon9

8:18 pm on Feb 9, 2016 (gmt 0)

10+ Year Member



We only have two sets of three pages that have been consolidated.

I have had a recommendation that rather than the regex to just use three separate "Redirect 301" statements.

It is our own server.

robzilla

10:04 pm on Feb 9, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For a few simple page-to-page redirects, a regex does seem a little wasteful, but realistically noone will notice the difference, and your time is probably better spent elsewhere. Even so, I'd prefer a single ^(one|two|three)\.php$ RewriteRule over three single ones; if you do use a regex, at least make it efficient.

whitespace

10:18 pm on Feb 9, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



2. I am wondering if a Redirect would be better. It is important to have the 301 redirect for SEO purposes.


Just to clarify, the RewriteRule with the R=301 flag is also a 301 redirect. The RewriteRule and the Redirect directives would have the same result in this respect. Just in case you were implying that the Redirect would be preferable because it's a 301 redirect.

I have had a recommendation that rather than the regex to just use three separate "Redirect 301" statements.


"the regex" in this instance being the RewriteRule (mod_rewrite) directive.

This is also Apache's recommendation. For simple redirects it is preferable to use Redirect, which is part of mod_alias. This is assuming you aren't already using mod_rewrite for other similar redirects - because you can end up with (confusing / hard to debug) conflicts.

mod_rewrite is a powerful beast that allows you to do far more complex redirects (and internal rewrites) than mod_alias (Redirect and RedirectMatch directives). Redirect (mod_alias) is simpler and that naturally comes with an increased efficiency.

Whether this "efficiency" is actually measurable in a real world scenario is another matter.

lucy24

3:39 am on Feb 10, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"the regex" in this instance being the RewriteRule (mod_rewrite) directive

Huh. My interpretation was that "the regex" meant
(one|two|three)\.php
as opposed to the three separate rules.

When it comes to a difference between parsing a RegEx and reading a few extra lines, it really is a matter of nanoseconds. And that's if it's in htaccess, where everything is read and evaluated over again on every request. If it's in the config file the single RegEx is probably faster, since it only has to be parsed once.

whitespace

8:28 am on Feb 10, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Huh. My interpretation was that "the regex" meant
(one|two|three)\.php


Well, yeah, I guess it does. Wires possibly getting crossed... I suppose I was looking at the "improved efficiency" of using Redirect (no regex) instead of RewriteRule (regex), which I would have thought would be the area of largest(*) gain. (And if combining the rules into a single regex, it would be preferable to use RedirectMatch if going the mod_alias route.)

(*) "largest" being very small.

jasimon9

2:19 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



1. The regex is simple. Exactly as (one|two|three)\.php

2. It is in httpd-ssl.conf in a vhost, not in htacess.

3. We are already using RewriteRule (with thanks to lucy24 for previous consults in 2012 and 2013!)

4. Seems the efficiency difference is not significant. So far based on the above, I would opt for the single line RewriteRule for simplicity for human readers.

lucy24

8:18 pm on Feb 10, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



for simplicity for human readers.

And that's absolutely a criterion. It applies to config files, php code, scripts, stylesheets, even the html itself. Think about what will be most intelligible to someone else coming along to work on the file later-- including yourself, 3 years down the line when you've forgotten what you meant.

Comments
# blahblah
/* blahblah */
//blahblah
<!-- blahblah -->
never hurt, either.

jasimon9

5:16 pm on Feb 11, 2016 (gmt 0)

10+ Year Member



Thank you for the input.

I have questions that relates to a bigger aspect of the project that this simple efficiency question was a small part of.

Back a ways in [webmasterworld.com...] you "shuffled papers" to reproduce the The Redirect-to-Rewrite Two-Step. Back then I based our rules on that method. My follow up questions are regarding "Part 0" (shown below). The questions are what does Part 0 accomplish and how critical is it?

We have converted all references on the site to the "short, pretty URLs". However, we have emails that go out that still include links to the old URL. We will eventually change them to the short, pretty URLs as well. But I was wondering what the harm might be to have them continue with the older forms for a while.


Part 0.
Before you do anything with Part 1 and Part 2, go over your current site carefully. Make sure that your own links pointonly to the short pretty URL. Requests for the long complicated URL should come only from outside-- from people with outdated bookmarks, or old links from other sites. Your own site will use only the pretty URLs.

lucy24

8:33 pm on Feb 11, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



what the harm might be

Not huge, but it's all part of the mix. I mean, it's not actively harmful to keep stepping around that open packing box you've got sitting in the middle of the floor because you haven't got around to breaking it down and tossing it in the recycling. But one of these years you'll get around to it.