


How to write the function of .htaccess file in apache_PHP tutorial
The functional writing method of .htaccess file in apache - .htaccess file (or "distributed configuration file" provides a method to change the configuration for a directory, that is, placing a file containing one or more instructions in a specific document directory to Applies to this directory and all its subdirectories. As a user, the commands you can use are restricted. Administrators can set it through the Apache AllowOverride directive. Directives in subdirectories will override higher-level directories or main server configurations. directives in the file.
- .htaccess must be uploaded in ASCII mode, preferably with its permissions set to 644.
Location of the error document
Common client request error return codes:
401 Authorization Required
403 Forbidden
404 Not Found
405 Method Not Allowed
408 Request Timed Out
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type
Common server error return codes:
500 Internal Server Error
Users can use .htaccess to specify their own pre-made error reminder page. , people can set up a special directory, such as errors, to place these pages. Then add the following instructions to .htaccess:
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/internalerror.html
One command per line. The first command above means that when the required document is not found, the page must be displayed as the notfound.html page in the /errors directory. It is not difficult to see that the syntax format is:
ErrorDocument. Error code/directory name/file name.extension
If there is very little information to be prompted, there is no need to create a special page and use the HTML number directly in the command, such as the following example:
ErrorDocument 401 "
You do not have permission to access this page , please give up!
"Password protection for document access
To use .htaccess to set access users and corresponding passwords for documents in a certain directory, the first thing to do is to generate a. The text document of htpasswd, for example:
zheng:y4E7Ep8e7EYV
The password here is encrypted. Users can find some tools to encrypt the password into the encoding supported by .htaccess. It is best not to place this document in the www directory. It is recommended to place it outside the www root directory document, which is safer.
With the authorized user document, you can add the following instructions to .htaccess:
AuthUserFile .htpasswd server directory
AuthGroupFile /dev/null (directory that requires authorized access)
AuthName EnterPassword
AuthType Basic (authorization type)
require user wsabstract (users allowed to access, if you want all users in the table to be allowed, you can use require valid-user)
Note, the brackets are comments added by yourself during learning
Deny access from a certain IP
If I don’t want a certain government department to access the content of my site, I can exclude them by adding the department’s IP in .htaccess.
For example:
order allow,deny
deny from 210.21.112.43
deny from 219.146.95
allow from all
The second line denies an IP, the third OK to deny a certain IP range, that is, 219.146.95.0~219.146.95.255
Want to deny everyone? Just use deny from all. Not only IP, but also domain name can be used to set it.
Protect .htaccess document
When using .htaccess to password-protect a directory, it contains the path to the password file. For security reasons, it is necessary to protect .htaccess so that others cannot see its contents. Although this can be done in other ways, such as permissions on the document. However, .htaccess itself can also do it, just add the following instructions:
order allow,deny
deny from all
URL redirection
We may redesign the website, move documents, or change the directory. At this time, visits from search engines or links from other websites may go wrong. In this case, you can use the following command to automatically redirect the old URL to the new address:
Redirect /old directory/old document name address of the new document
or redirect the entire directory:
Redirect old Directory New Directory
Change the default homepage file
Generally, the default homepage file names include default, index, etc. However, sometimes there is no default file in the directory, but a specific file name, such as pmwiki.php in pmwiki. In this case, it is troublesome for the user to remember the file name to access it. You can easily set a new default file name in .htaccess:
DirectoryIndex You can also list multiple new default file names
, and the order indicates the priority between them, for example:
DirectoryIndex filename.html index.cgi index.pl default.htm
in a specific document directory

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.
