.htaccess rewrite

I want to prohibit anyone from directly accessing files in a particular directory and was planning to use .htaccess rewrite rules. The directory I wish to protect is www.domain.com/ebookshop/pay2see and I was going to redirect anyone trying to access files in the pay2see directory to [URL=“http://www.domain.com/ebookshop/pay2see.html”]www.domain.com/ebookshop/pay2see.html
I thought the following would do the trick but it appears not:

RewriteRule ^pay2see/?$ /ebookshop/pay2see.html [PT,L]

Can anyone help please? Thanks G :slight_smile:

You can do this via htaccess. Or another easy way is to just password protect the folder via your cpanel. And no google cant see it either.

The problem is this pattern will match only /pay2see/, not /pay2see/some-path or /pay2see/some-path/more-path. If you want to match “pay2see” plus any paths underneath it, then you’ll have to write it like this:

^pay2seeCOLOR=“#FF0000”?[/COLOR]$

Or, alternatively:

^pay2seeCOLOR=“#FF0000”[/COLOR]

Also, it matters where the htaccess file lives. All these patterns will work only if the htaccess file is inside “ebookshop”. If, instead, your htaccess file lives at the root, then you’ll have to include “ebookshop” in the pattern.

[1]ebookshop/[/COLOR]pay2see(?:/|$)


  1. COLOR=“#FF0000↩︎

Ah - thanks Jeff. I’ll try that… G :slight_smile:

I’ve tried

RewriteRule ^ebookshop/pay2see/?$ /ebookshop/pay2see.html [PT,L]

as there are no subdirectories but it still doesn’t seem to work. Am I being dense?

For the sake of completeness my entire .htaccess file is

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^ebookshop/pay2see/?$ /ebookshop/pay2see.php [PT,L]
IndexIgnore *
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php

And what’s an example URL that you’re accessing that you expect to match but isn’t?

www.domain.com/ebookshop/pay2see/book.pdf

  • it’s the dot isn’t it!

Actually the problem is still the same that I mentioned before. You’re matching exactly /pay2see/ and not allowing for anything to come after in the URL.

Ah - thanks. I thought that was only if I had subdirectories. All working now.

Thanks again
G :slight_smile: