将 .html 添加到 url 但未找到结果 404
add .html to url but results 404 not found
阅读 this 和 bababa,但遗憾的是 none 可以指导我如何调试。
我正在尝试重定向到下面 url
https://example.com/hello
至
https://example.com/hello.html
这是我的做法
第一
vim/etc/apache2/apache2.conf
,在<Directory /var/www/>
中设置AllowOverride All
现在是
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
第二
sudo service apache2 restart
第三
在 public_html
及以下代码
中创建 .htaccess
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html
</IfModule>
但还是return 404 没有找到,如下所示。
我在配置 Apache 时缺少任何建议或任何部分吗?谢谢
------答案------
缺少 mod_rewrite
的配置。可以检查这个post
您在 %{REQUEST_FILENAME}
的末尾添加 .html
这将导致 404,因为 %{REQUEST_FILENAME}
表示完整的文件系统路径而不是 URI 路径。
你应该使用这条规则:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{REQUEST_URI}.html [L]
甚至更好:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ .html [L]
阅读 this 和 bababa,但遗憾的是 none 可以指导我如何调试。
我正在尝试重定向到下面 url https://example.com/hello 至 https://example.com/hello.html
这是我的做法
第一
vim/etc/apache2/apache2.conf
,在<Directory /var/www/>
AllowOverride All
现在是
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
第二
sudo service apache2 restart
第三
在 public_html
及以下代码
.htaccess
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html
</IfModule>
但还是return 404 没有找到,如下所示。
我在配置 Apache 时缺少任何建议或任何部分吗?谢谢
------答案------
缺少 mod_rewrite
的配置。可以检查这个post
您在 %{REQUEST_FILENAME}
的末尾添加 .html
这将导致 404,因为 %{REQUEST_FILENAME}
表示完整的文件系统路径而不是 URI 路径。
你应该使用这条规则:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{REQUEST_URI}.html [L]
甚至更好:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ .html [L]