大网数据广告活动
网站运营与故障问答

Rewrite rules in .htaccess never take effect and no redirects ever happen

· 52CE 我爱测

I recently set up a vps with digital ocean to let my site run on php7 and http2 Everything is running great but my sites .htaccess file is not redirecting as it should. I have changed up my 000-default.conf and my apache2.conf fi…

问题原文

原帖链接 · Phillip Dews · CC BY-SA 3.0

I recently set up a vps with digital ocean to let my site run on php7 and http2 Everything is running great but my sites .htaccess file is not redirecting as it should. I have changed up my 000-default.conf and my apache2.conf files to allow .htaccess to work!

This is what I have in my .htaccess file

#ExpiresActive On
#ExpiresByType image/gif "now plus 9 years"
#ExpiresByType image/png "now plus 9 years"
#ExpiresByType image/jpeg "now plus 9 years"
#ExpiresByType image/x-icon "now plus 9 years"

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://example.com [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com [L,R=301]

RewriteBase /
RewiteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$1 [R=301,NE,L]

On my other shared hosting this file worked fine and I know this file is working because if I uncomment all of the ExpiresByType it breaks my site so I will probably take those out! Also the index.php does not redirect. Have I missed something somewhere?

Have been told that my question is unclear so will provide my sites .conf file.

DirectoryIndex index.php
<LocationMatch "^(.*\.php)$">
    ProxyPass fcgi://127.0.0.1:9000/var/www/example.com/public_html
</LocationMatch>
NameVirtualHost *:443
<VirtualHost *:443>
    AccessFileName .htaccess
    ServerName www.example.com
    ServerAlias www.example.com

    SSLEngine on
    SSLCertificateFile "/etc/letsencrypt/live/example.com/cert.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/example.com/privkey.pem"
    SSLCertificateChainFile "/etc/letsencrypt/live/example.com/chain.pem"

    Protocols h2 http/1.1
    H2Direct on
    ServerAdmin [email omitted]
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews Includes
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

I can also confirm that sudo a2enmod rewrite is enabled. Hope this clears things up.

已采纳解答

原帖链接 · Phillip Dews · CC BY-SA 3.0

I finally figured it out and it was really simple. Instead of modifying my sites virtual hosts I file modified my apache2.conf file located at /etc/apache2/apache2.conf as such

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

Took me a while to get there but simple when you know how! Thanks for all your help

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。