apache .htaccess重写-我可以将其移到httpd.conf中吗

f0brbegy  于 2022-11-16  发布在  Apache
关注(0)|答案(3)|浏览(139)

下面是我在apache2.2的.htaccess文件中的唯一代码。
我读到使用.htacess会影响性能,如果这个可以在httpd.conf中运行,效果会更好。因此,我可以将这个添加到httpd.conf中吗?如果可以,我应该将它放在哪里?
它是否需要进入每个需要它的主机的VirtualHost(在我的例子中是很多主机),或者它是否可以一般地进入httpd.conf,以便应用于所有主机?

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
kxe2p93d

kxe2p93d1#

.htaccess提供目录配置,而httpd.conf提供整体配置。当然,您可以将内容从.htaccess移动到httpd.conf。您可以在此处找到有关.htaccess的更多信息:Apache HTTP Server Tutorial: .htaccess files
以您的.htaccess为例:
/www/htdocs/example中.htaccess文件的内容

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

节的内容。

<Directory /www/htdocs/example>
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
</Directory>
svmlkihl

svmlkihl2#

每个VirtualHost都需要指定自己的重写规则,除非:
1.在每个VirtualHost中指定RewriteOptions inherit,或者
1.在每个VirtualHost中使用include指令
来源:https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#RewriteEngine
请注意,虚拟主机不会继承重写配置

huwehgph

huwehgph3#

根据Apache文档,您可以将配置放在<Directory>-block. http://httpd.apache.org/docs/2.2/howto/htaccess.html中的.htaccess中
如果您可以访问httpd主服务器配置文件,则应完全避免使用.htaccess文件。使用.htaccess文件会降低Apache http服务器的速度。您可以在.htaccess文件中包含的任何指令最好设置在Directory块中,因为这样做会产生相同的效果,但性能更好。
为了简化管理,我会把重写指令放在一个单独的文件中,然后将其包含在每个虚拟主机中。

相关问题