I have a client that strongly prefers to disable .htaccess files because they like to set the Apache configurations themselves. However, they still want SEO-friendly URLs.
Is there a way to have custom permalinks with no .htaccess file? My research thus far seems to indicate this is not possible, but perhaps one of brilliant developers knows how the seemingly impossible can be possible. Thanks in advance!
Hi @Mike Lee:
To answer your question it is helpful to understand how everything works.
Apache Serves URLs that Match Files and Directories
Apache is designed to serve files explicitly matched by URL, or to serve the
index.php
found in a directory when the directory is explicitly matched.But Apache Can Serve URLs Matched by Regex with
mod_rewrite
If you want Apache to match URLs for where there are no real directories (the case with WordPress and pretty permalinks) then you must have some way to tell Apache how to handle URLs differently. And that’s exactly what
mod_rewrite
was designed to allow; it gives server administrators the ability to set rules for matching URLs using regular expressions. Those rules route the result to other URLs, often including actually.PHP
files and sometimes with URL parameters passed. Ultimately the rules specify that actual files get loaded.And
mod_rewrite
is Configured with Either.htaccess
orhttpd.conf
To configure
mod_rewrite
you can only do it inside.htaccess
or within thehttpd.conf
file or one of the files it includes, like potentiallyhttpd-vhosts.conf
. Actually I’m surprised if you client has the skills to control Apache that they don’t know this already.WordPress Always Uses the Same Simple
.htaccess
FileMoving on to what WordPress does, when you set permalinks WordPress writes the following to the
.htaccess
file, assuming it is writable (and in this first example assuming your website site is served from the root):Caveat: When Your WordPress Front Page Directory is Not Root
If your site is instead served from
/blog
then the.htaccess
file written would look like this:WordPress Routes All Non-File/Directory Matching URLs to
index.php
So as you can see, the only thing WordPress uses
.htaccess
for is to map any URL to the domain to/index.php
(or/blog/index.php
in the 2nd example) except when a URL matches an actual file (such as an.jpg
/.gif
/.png
image, a.css
stylesheet, a.js
script, etc.) or when it matches an actual directory (which as far as I know is not relevant in a standard WordPress install.)In
PHP
WordPress Parses$_SERVER['REQUEST_URI']
To Decide What to LoadInside its
PHP
code WordPress grabs the value of$_SERVER['REQUEST_URI']
which contains the full URL request sans the domain and scheme (i.e. scheme ishttp
orhttps
) and it then parses the value to determine what URL was requested and thus what pages it should load.Bypassing
.htaccess
? Get Apache to Load Virtual URLs (but good luck with that!)So if you want to somehow bypass
.htaccess
your job would be to get Apache to respond to an arbitrary URL then load WordPress and set$_SERVER['REQUEST_URI']
to be the URL path plus parameters; IOW spoofing it but in a good way. That said, I know if know of no ways that are not overly complicated to do that.Embedding
/index.php/
(Maybe?!?)Even though *Chris_O* is correct about prepending
/index.php/
to your URLs I cringe whenever I see that. It adds 10 characters to every URL making them longer and less meaningful to search engines but far worse makes them less sharable and looks cryptic to users. Sorry Chris I know you meant well, but ugh!Create Real Directories for Every URL (Maybe?)
One way to get pretty permalinks without touching Apache would be to write a script that would generate an actual directory for every URL that you want and then store an
index.php
there that would load WordPress. Of course that would be huge effort for tiny benefit and it would require the server to have write access which has to be worse than using an.htaccess
file.I hate to admit but this is what I did circa 1998 with an
.ASP
-based website whenIIS
didn’t support URL rewriting (and even today it’s still a real PITA!) It was an ugly hack, was a pain to maintain and I hated it but the URLs sure were great for both users and for SEO!Best Solution? Add Rewrite Rules to
httpd.conf
Back to what’s probably your best the solution, and @Simon Brown actually recommended it; add your rewrite rules to
httpd.conf
or one of the include files likehttpd-vhosts.conf
(which is how Apache is configured at localhost on my Mac.) Add the following directive making sure to change the directory to match the directory for your site:Bonus! With Lockdown Comes Performance Too
This last option should eliminate any
.htaccess
and put control back in their hands.Even better it’s slightly more performant since
httpd.conf
is only loaded once when Apache starts but.htaccess
files are loaded and parsed on every URL request!P.S. One more thing to consider would be to front-end Apache with a caching server like Nginx which I believe is becoming a best practice for high-traffic WordPress sites that really need to be performant. It might take green field tweaking because I don’t think most people have been using Nginx to do the URL rewriting for Apache but if that direction interests you here are some links to pursue:
Permalinks without mod_rewrite
Without an .htaccess file or modifying your httpd.conf file the best you can do is pathinfo permalinks. Pathinfo permalinks are the same as pretty permalinks except they start with index.php.
To use pathinfo permalinks put index.php/ at the start of your custom permalink structure:
See the Codex article for more information.
Back in the Bad Old Days, WordPress needed to write a new config file every time you changed the permalink structure. In modern setups, the RewriteRules are unchanging:
All requests for non-existant files (ie. a custom permalink path that doesn’t match a file on the filesystem) gets passed through index.php, and
$_SERVER['REQUEST_URI']
tells PHP what was actually requested. Your client can set the rewrite rules in httpd.conf or .htaccess, and you won’t need to modify it as you tweak the permalink structure.Some plugins do attempt to modify .htaccess themselves, or ask you to modify the file. This solution may not be for everybody, but it’s worth considering.