Redirect all requests to .doc & .docx

Hi Guys!

How do I redirect all requests to .doc and .docx to a custom url. For example:

would get redirected to:

Thanks in advance.

Any suggestions…?


Aw, you’ve been in here before (when I was the TL) so you know I’ll ask whether you’ve read the sticky threads (which cover this sort of topic) or the mod_rewrite tutorial Article linked in my signature (which is the origin of the sticky threads - but maintained).

Basically, examine the URI for the docx? (optional trailing x) file extension and capture whatever you need of the URI before (including?) the docx? extension and perform your redirection. What can be simpler than that? Oh, yeah, doing something online with a Word document! How do you plan to serve that (other than as a download)?



Hi DK,

I’m just looking to redirect the users to a download page which will automatically start the word document download. This requires the user to have an account and the htaccess will also stop people trying to steal the word document without having an account.


It looked like that (without the security information being provided). SMART. With the above information, you now know how to deal with the docx? file extension (okay, you already KNEW that so this was good information for others).



I actually have no idea how to deal with the doc or docx. Im trying to find some examples of this kind of thing online, but i’ve found nothing. It’s back to the drawing board for me…

Redirecting in .htaccess or forcing the download?

Redirecting in .htaccess…

I know how to force the download easily in PHP.

Redirecting especially for doc or docx is no different than any other redirect, so don’t think of it as a special case (it sounds like you are). It’s just one URL in, another URL out. That the URLs happen to have doc/docx in them doesn’t matter at all to Apache.

In words, the redirect (RewriteRule) would be:

if the URL ends in doc or docx (i.e. docx? in a regular expression), rewrite to another URL.
That, together with the tutorial dklynn pointed you too (Apache’s mod_rewrite) should be enough to get you started no?

FYI. What you want is possible, no need to grab your drawing board just yet.

Ok, so Im trying the following code to redirect all .doc/.docx requests, but it doesn’t work. What do I need to change?

RewriteRule users/uploads/cvs/(.*) downloadcv.php?cv=$1 [NC,L]

I assume that you have RewriteEngine On in your .htaccess somewhere?

Also, you start the matching URL with a ^ and only match valid filenames instead of using the dreaded (.*)

So, something like

RewriteEngine On
RewriteRule ^users/uploads/cvs/([a-zA-Z0-9_-]+\\.docx?)$ downloadcv.php?cv=$1 [NC,L]

Hi ScallioTX,

Thanks for your help on this. I have changed my programming technique and I would now like to show a 403 error to the user if they try to access the doc or docx files (instead of redirecting to downloadcv.php). Is this easily achieveble?


Yes, just replace [NC,L] with [F]