php 如何在 Apache 2.4 上启用 mod_rewrite?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30855651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:06:47  来源:igfitidea点击:

How to enable mod_rewrite on Apache 2.4?

phpwordpressapachemod-rewritecentos7

提问by Jonathan Solorzano

I'm using Wordpress and wanted to enable pretty urls feature, but they don't work, i'm thinking that it's because of mod_rewrite, but i don't know how to enable it on Apache 2.4 under CentOS 7...

我正在使用 Wordpress 并希望启用漂亮的 url 功能,但它们不起作用,我认为这是因为 mod_rewrite,但我不知道如何在 CentOS 7 下的 Apache 2.4 上启用它...

I've already try this:

我已经尝试过这个:

grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite

but nothing... Also would like to know it loading all Apache modules has any bad consequence or is it bad practice?

但什么都没有......还想知道它加载所有Apache模块有什么不好的后果还是不好的做法?

回答by Jonathan Solorzano

I found the way to know if a module is loaded or not, here's to command to list the modules enabled:

我找到了知道模块是否已加载的方法,这里是命令列出已启用的模块:

apachectl -M | sort

It will list all enabled modules alphabetically.

它将按字母顺序列出所有启用的模块。

Wordpress has an .htaccess but default where it enables rewrite_modulefor its use:

Wordpress 有一个 .htaccess,但默认情况下它可以rewrite_module使用:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The only thing that i had to do was add this to the vhost.conf file at /etc/httpd/conf.d/vhosts.conf

我唯一要做的就是将它添加到 vhost.conf 文件中 /etc/httpd/conf.d/vhosts.conf

<Directory /var/www/mysite>
    Options Indexes FollowSymLinks
    Require all granted
    AllowOverride All
</Directory>

That's because I'm handling my hosts in that file. But it could be done at httpd.conf, or any other .conffile that is Included to the httpd.conf

那是因为我在那个文件中处理我的主机。但它可以在httpd.conf,或.conf包含在httpd.conf

Thanks...

谢谢...

回答by marekful

Apache2 ships with executables a2enmodand a2dismodthat will do all the "dirty work" of symlinking conf files.

Apache2中附带的可执行文件a2enmod,并a2dismod会做符号链接conf文件中所有的“脏活”。

On Debian based distros, the usual location is /etc/apache/mods-available/. Here are the .conf configuration files (when applicable) and a .load file per module with the LoadModuleApache2 directive. A module is enabled if there is a symlink to its .load file in the /etc/apache2/mods-enabled/.

在基于 Debian 的发行版上,通常的位置是/etc/apache/mods-available/. 这是 .conf 配置文件(如果适用)和每个模块的 .load 文件,带有LoadModuleApache2 指令。如果 .load 文件中存在指向模块的符号链接,则启用模块/etc/apache2/mods-enabled/

You would, for example, enable the Rewrite module with command

例如,您可以使用命令启用重写模块

$ a2enmod rewrite

回答by reimi

# nano /etc/httpd/conf/httpd.conf

find

follow (AllowOverride none) 

and change it

并改变它

(AllowOverride All) 

回答by Kent Aguilar

In httpd.conf, search for AllowOverride Noneand change it to AllowOverride All, then restart apache

httpd.conf 中,搜索AllowOverride None并将其更改为AllowOverride All,然后重新启动 apache

回答by camillo777

Yes, changing from "Override None" to "Override All" made it work:

是的,从“无覆盖”更改为“全部覆盖”使其工作:

On Debian 10 config file is here:

在 Debian 10 配置文件在这里:

/etc/apache2/apache2.conf

/etc/apache2/apache2.conf

I don't know the security risks, but it works!

我不知道安全风险,但它有效!

回答by Rao

For CentO 7 Only :a2enmodis a Debian/Ubuntucommand. You'll need to edit the Apache configfile for a CentOSserver.

仅适用于 CentO 7:a2enmodDebian/Ubuntu命令。您需要为CentOS服务器编辑Apache 配置文件。

What you'd want to do is edit the /etc/httpd/conf/httpd.conffile and make sure that the AllowOverrideflag for the block is changed from Noneto All.

您想要做的是编辑/etc/httpd/conf/httpd.conf文件并确保块的AllowOverride标志从None更改为All

Basically it should be something like this: (Note: there are many comment lines in the default httpd.conf file)

基本上应该是这样的:(注意:默认的httpd.conf文件中有很多注释行)

<Directory "/var/www/html"> AllowOverride All </Directory>

<Directory "/var/www/html"> AllowOverride All </Directory>

回答by Hassan Saeed

for rewrite module just check this youtube step by step tutorial related to enable rewrite module in wamp apache https://youtu.be/xIspOX9FuVU?t=1m43s
Wamp server icon -> Apache -> Apache Modules and mark as check the rewrite module option
after that you can able to use url rewriting

对于重写模块,只需查看与在 wamp apache 中启用重写模块相关的 youtube 分步教程https://youtu.be/xIspOX9FuVU?t=1m43s
Wamp 服务器图标 -> Apache -> Apache 模块并标记为检查重写模块选项
之后你可以使用url重写