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.
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>
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
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>
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
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
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!