How to protect specific URL in Apache

不言
Release: 2023-04-05 19:24:01
Original
2324 people have browsed it

Sometimes we need to protect one or a few specific URLs in our website and all other website URLs remain public access. It is very easy to manage using the directory and file structure in the site, but the routing structure of frameworks such as cakephp is different from the directory structure, and we cannot protect it at the directory level. This article will introduce protecting specific URLs in Apache.

How to protect specific URL in Apache

For example, a site has a secure area like http://example.com/admin/", we only have authorized users or IPs to access the /admin/ section.

1. Set IP-based restrictions on specific URLs

First edit the apache configuration file and add the following entries in virtualhost. This will only allow /admin URLs Access the 192.168.10.11 and 123.45.67.89 IPs.

<Location /admin>
  Order deny,allow
  Deny from all
  Allow from 192.168.10.11
  Allow from 123.45.67.89
</Location>
Copy after login

Save the Apache configuration file and restart the Apache service using one of the following commands.

# service httpd restart          #  For RHEL based systems
$ sudo service apache2 restart    # For Debian based systems
Copy after login
Copy after login

We try to access your site from any other IP. Also check the given ip in the configuration file.

2. Set user authentication on a specific URL

Edit the apache configuration file and add it to the website Add the following content to the virtualhost section.

<Location /admin>
  AuthUserFile /var/www/htpasswd/.htpasswd
  AuthName "Password Protected Area"
  AuthType Basic
  Require valid-user
</Location>
Copy after login

Now create a new htpasswd file using the command below and add a new user.

# htpasswd -cm /var/www/htpasswd/.htpasswd myuser

New password:
Re-type new password:
Adding password for user myuser
Copy after login

Restart Apache and visit your website URL, it will prompt for login Detailed information.

# service httpd restart          #  For RHEL based systems
$ sudo service apache2 restart    # For Debian based systems
Copy after login
Copy after login

This article has ended here. For more exciting content, you can pay attention to the PHP Video Tutorial column on the PHP Chinese website!

The above is the detailed content of How to protect specific URL in Apache. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template