How can I have a different htaccess for every page that I have?

Hello,I need to restrict access to my site’s pages by direct url,and I have written the code,it works fine but because I have pages,the code that I put in the htaccess file gets pasted in all the other pages and breaks the code.
So my question is, how can I make it so each page has its own htaccess or make the code only work for each site that I paste it on?

Password protection with .htaccess files works on the directory level. If you configure it for a certain directory, then all pages, subdirectories and the pages in those subdirectories will require a valid username and password to access.

This is just how directory protection works and you cannot manage access control for specific files with this system.

If you don’t like the way your pages are protected now, here are a few options:

  • You can restructure your files and paths so you can work with these limitations. So instead of trying to protect an admin.php file and user.php file separately, you could instead create admin/index.php and user/index.php so you can control access per directory.
  • If you want more fine grained access control, you can implement authentication with PHP instead. Through PHP code, you can control exactly how people can sign up, login, be invited, manage their own profiles, and implement any access control and authorization checks you want.

Please understand that directory protection isn’t that flexible and isn’t a good tool to implement a real authentication system.

5 Likes

Thank you for the reply but this is not what i am asking for,please re read the message and answer me correctly.

Here’s the code that I used and worked:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://subreach.kesug.com/ [NC]
RewriteRule ^(.*)$ - [F]

I read your message again, and while I now understand the issue you had by looking at the solution, I can with complete confidence tell you that I would have never guessed that this would have been the solution to the issue you had.

And to be honest, regarding the question “different .htaccess per page”, the code you have just applies this check to all URLs in the directory. So there is absolutely no different .htaccess per page there.

So I’m glad that you were able to solve it, because I wouldn’t have been able to with the vague description you have provided.

Please note that hotlinking is not supported on our hosting by default, due to this system:

Whether the solution you have works as intended depends on the exact use case, which I don’t fully understand. However, you should know that referrer headers are very easily to manipulate, and may not do much for “security” to begin with.

4 Likes

This is not the solution I need,I provided the code to show you what I use,and the problem is that in my 100 pages,when i paste it into 1 page it goes to all of them,i need a way to “bypass” that.

You cannot. .htaccess files apply to an entire directory, which from the servers perspective counts as one website. There is no way to use separate htaccess files for each individual page. You can use a protected directories rule to only secure specific pages (see this guide here), but based on your description, I’m not sure you can do what you want that way. The admin gave you some good alternative suggestions earlier.

The former would be the easiest to implement, but the latter would be more flexible (and potentially more secure in the long run, depending on how you set it up).

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.