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

时间:2019-04-18 11:04:12

标签: .htaccess

我曾经经营过一个Wordpress网站,但后来开始用php构建自己的网站。我在服务器的子目录上构建它,现在想启动新网站。结构如下:

public_html / newversion / index.php

public_html / newversion / category.php

public_html / newversion / category / information.php

我已经编辑了public_html中的.htaccess文件以强制使用非www和https,将newversion设置为新的文档根目录,并提供了一些代码以从URL中删除.php扩展名(请参见下面的.htaccess代码)。 / p>

这意味着我希望当有人去example.com而不是需要去example.com/newversion/index.php的人显示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>

1 个答案:

答案 0 :(得分:0)

花了我很长时间才弄清楚这个问题,但是我现在知道人们用.htaccess重写伏都教的含义了...因此,对于仍然需要答案的任何人:

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

基础代码实现以下目的:

  • 子目录中的网站根目录

  • 强制非www和https

  • 从网址中删除.php扩展名

  • 打开子目录为example.com/foo/,无论是否以斜杠输入

  • 打开子目录中的文件,例如example.com/foo/bar,是否以斜杠输入

.htaccess文件位于public_html中:

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