mod_rewrite 在 Apache 2.4 中的使用

Tji*_*ta1 7 mod-rewrite apache2.4

好的,关于如何使用 mod_rewrite 有很多线程,但我发现它在 Apache 2.4 中的工作方式似乎有所不同。我试过这个:

<Directory /var/www/vhosts/example.com>
<IfModule mod_rewrite>
RewriteEngine On
RewriteBase /var/www/vhosts/example.com
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
</Directory>
Run Code Online (Sandbox Code Playgroud)

这是位于/etc/apache2/conf-available/httpd.conf/,我已经矮了sudo a2enconf httpd.conf运行并启用了它,但它无论如何都不想工作..

我没有尝试过Directory,我尝试按照mod_rewrite.cwordpress建议的方式调用它,但上述两种方法似乎都不起作用。我也尝试过使用Directory标记但没有使用IfModule,由于显而易见的原因,最终导致 Apache 无法启动.. 是的,我在每次尝试之间重新启动 Apache。

Apache 2.4 中是否有我不知道的新技巧?任何帮助表示赞赏。

met*_*rph 11

通过此命令启用 mod_rewrite后:

sudo a2enmod rewrite
Run Code Online (Sandbox Code Playgroud)

/etc/apache2/sites-available/ 中为您的httpd 配置制作文件,因此在我的 Ubuntu 中,我制作的文件是/etc/apache2/sites-available/code-machine.conf

DocumentRoot /var/www
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

编辑:正如 Chris Gregory 和 firepol 所提醒的,您可以注册新的配置文件code-machine.conf

sudo a2ensite code-machine.conf
Run Code Online (Sandbox Code Playgroud)

然后重启你的 apache

sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)