apache .htaccess rewrite - can I move this into httpd.conf

半世苍凉 提交于 2019-12-05 23:31:14

问题


Below is the only code I have in the .htaccess file with apache 2.2.

I've read that its a performance impact to use a .htacess and better if this this can be run out of httpd.conf. Therefore is it possible for me to add this into httpd.conf? If so where would I put it?

Would it need to go into the VirtualHost for each Host that needed it (in my case a lot of hosts) or could it go generically into httpd.conf so it applies to all hosts?

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

回答1:


.htaccess provides configuration for a directory, while httpd.conf provides an overall configuration. Of course, you can move content from .htaccess to httpd.conf. You can find more about .htaccess here: Apache HTTP Server Tutorial: .htaccess files

Take your .htaccess for example:

Contents of .htaccess file in /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>

Section from your httpd.conf file

<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>



回答2:


Each VirtualHost needs its own rewrite rules specified, unless:

  1. You specify RewriteOptions inherit within each VirtualHost or
  2. You use an include directive within each VirtualHost

SOURCE: https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#RewriteEngine

Note that rewrite configurations are not inherited by virtual hosts




回答3:


According to the Apache docs you may put the configuration in a .htaccess within a <Directory>-block. http://httpd.apache.org/docs/2.2/howto/htaccess.html

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

To ease the administration I would put the rewrite directives in a separate file that is then included in each virtual host. It ought to work.



来源:https://stackoverflow.com/questions/17140605/apache-htaccess-rewrite-can-i-move-this-into-httpd-conf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!