404 issue in two htaccess in root domain and subfoler

旧城冷巷雨未停 提交于 2021-02-19 03:58:21

问题


My root domain htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteCond %{`HTTP_HOST`} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ `https://%1/$1` [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) `https://%{HTTP_HOST}%{REQUEST_URI}` [R=301,L]

In my subfolder htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]

Wehn I type in example.com/any-wrong-url, it will redirect to 404 page, that is correct.

But when I type in example.com/subfoler/any-wrong-url it can not redirect to 404 page. Where do I something wrong?


回答1:


The best way to do this without creating any conflict or redirect is:

Use /subfolder/ instead of root / on the RewriteBase like this:

RewriteBase /subfolder/

Copy the 404.php inside the subfolder.

The complete code will look like:

RewriteEngine On
RewriteBase /subfolder/
ErrorDocument 404 /404.php
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]

This should work.



来源:https://stackoverflow.com/questions/52759801/404-issue-in-two-htaccess-in-root-domain-and-subfoler

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