In order to use mod_rewrite
you can type the following command in the terminal:
(要使用mod_rewrite
您可以在终端中键入以下命令:)
sudo a2enmod rewrite
Restart apache2 after
(之后重启apache2)
sudo /etc/init.d/apache2 restart
or
(要么)
sudo service apache2 restart
or as per new unified System Control Way
(或者按照新的统一系统控制方式)
sudo systemctl restart apache2
Then, if you'd like, you can use the following .htaccess
file.
(然后,如果您愿意,可以使用以下.htaccess
文件。)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The above .htaccess
file (if placed in your DocumentRoot
) will redirect all traffic to an index.php
file in the DocumentRoot
unless the file exists.
(上面的.htaccess
文件(如果放在DocumentRoot
)会将所有流量重定向到DocumentRoot
的index.php
文件,除非该文件存在。)
So, let's say you have the following directory structure and httpdocs is the DocumentRoot
(所以,假设您有以下目录结构,httpdocs是DocumentRoot
)
httpdocs/
.htaccess
index.php
images/
hello.png
js/
jquery.js
css/
style.css
includes/
app/
app.php
Any file that exists in httpdocs will be served to the requester using the .htaccess
shown above, however, everything else will be redirected to httpdocs/index.php
.
(httpdocs中存在的任何文件都将使用上面显示的.htaccess
提供给请求者,但是,其他所有文件都将重定向到httpdocs/index.php
。)
Your application files in includes/app
will not be accessible. (您的应用程序文件includes/app
将无法访问。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…