Rewriting URLs without Apache - a reversal
Written by Richard Heyes, RGraph author, on 6th August 2022
You may remember me writing about
a new way of doing rewrites - without the Apache mod_rewrite module.
Well, after a year or so of using this method I've
decided to revert back to the standard way of doing
Apache
rewrites and redirects - ie using the
RewriteRule
directive (as well as the
RedirectPermanent
directive too).
Why?
Living with the new method just proved to be "not quite as simple" as using traditional methods with too many things to keep in mind. For example, the script name wasn't the same as the request URI - not a problem really - but something to remember and if you didn't you could easily get caught out.
Also, when I originally blogged about this method,
I noted that performance wasn't an issue unless you
had thousands of redirects. Now, I don't have any
hard numbers to support this, but I do have a
"feeling" (based on observations) that using the
Apache
directives is
faster in practice than the new method. I think that
I can note
a very slight improvement - possibly due to
there being no massive php
file to parse. But, as
with a lot of things, this may be just in my head!
So how have you laid out the redirects?
All the redirects are now placed in a single
.htaccess
file (previously they were
in seperate files in each directory),grouped by
directory and commented clearly to make finding
things easy. For example:
################################ # Redirect regular expressions # ################################ RedirectMatch /canvas/docs/adjusting-linehtml.* /canvas/adjusting-line.html RedirectMatch ^/demos/bar-basic.html.+ /demos/bar-basic.html RedirectMatch ^/fiddle.* /demos/index.html # ... ################ # REDIRECTS: / # ################ RedirectPermanent /interactive /demos/index.html RedirectPermanent /interactive/ /demos/index.html RedirectPermanent /browser /install/browsers.html # ... #################### # REDIRECTS: /blog # #################### RedirectPermanent /blog/index.html/index.html /blog/index.html RedirectPermanent /blog/index.html/2011 /blog/index.html RedirectPermanent /blog/index.html/2012 /blog/index-2012.html # ... ###################### # REDIRECTS: /canvas # ###################### RedirectPermanent /canvas/funnel /canvas/funnel.html RedirectPermanent /canvas/misc.html/ /canvas/misc.html RedirectPermanent /canvas/interactive-keys.html /canvas/keys.html#interactive-keys
Not too difficult to maintain and quick. What everyone wants right?