将public_html子文件夹重定向404错误作为网站的根文件夹

问题描述 投票:0回答:1

我曾经运行过Wordpress网站,但是开始在php中构建自己的网站。我在我的服务器上的子目录中构建它,现在想要启动新网站。结构如下:

的public_html / NEWVERSION / index.php的

的public_html / NEWVERSION / category.php

的public_html / NEWVERSION /分类/ information.php文件

我在public_html中编辑了.htaccess文件以强制非www和https,将newversion设置为新文档根,以及从URL中删除.php扩展名的一些代码(请参阅下面的.htaccess代码)。

这意味着我希望newversion / index.php出现在有人去example.com时,而不是需要去example.com/newversion/index.php的人。

一切正常,除非我尝试访问category.php或information.php。然后浏览器自动重定向到example.com/newversion/category.php,我收到404错误。当我尝试访问information.php时,浏览器尝试打开example.com/category/information.php,但我也收到404错误。

我究竟做错了什么? (仅供参考,Wordpress不再安装)

    # Apache Rewrite Rules
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    # Base Redirects #

    # Remove trailing slash from non-filepath urls
    RewriteCond %{REQUEST_URI} /(.+)/$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ https://example.com/%1 [R=301,L]

    # Include trailing slash on directory 
    RewriteCond %{REQUEST_URI} !(.+)/$
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]

    # Force HTTPS and remove WWW
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
    RewriteCond %{https} off  
    RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

    # Change root folder to subfolder of public_html
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -d [OR]
    RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -l 
    RewriteRule ^ /newversion%{REQUEST_URI} [L]

    RewriteEngine On

    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [R=301,L]

    # Resolve .php file for extensionless php urls
    RewriteRule ^([^/.]+)$ $1.php [L]

    # Set php version
    AddHandler application/x-httpd-recommended-php .php .php5 .php4 .php3

    </IfModule>

    # Restrict access
    <Files .htaccess>
    order allow,deny
    deny from all
    </Files>
.htaccess
1个回答
0
投票

我花了很长时间搞清楚这一点,但我现在知道人们的意思.htaccess重写伏都教......那么,对于任何仍需要答案的人:

最后,声明htaccess规则的顺序和执行它的子目录非常重要。不要问我它是如何工作的,因为我不完全理解。但是,这段代码对我有用。

底层代码实现了以下功能:

  • 网站根目录在子目录中
  • 强制非www和https
  • 从url中删除.php扩展名
  • 打开子目录,如example.com/foo/,是否使用尾部斜杠输入
  • 无论是否使用尾部斜杠输入,都会以example.com/foo/bar打开子目录中的文件

public_html中的.htaccess文件:

# htaccess for public_html

Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php

# Remove www and force https
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/subdirectory/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301]

#Prevent direct access to PHP Scripts
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
RewriteRule ^ - [R=404,L]

RewriteRule ^/?$ subdirectory/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!subdirectory/).*)$ subdirectory/$1 [L,NC]

然后在子目录中添加以下htaccess文件:

# htaccess for subdirectory

# Remove trailing slashes (keep as first rule!)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]

# Open page without php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
© www.soinside.com 2019 - 2024. All rights reserved.