【问题标题】:.htaccess file returning 404 error when redirecting the root folder to a file.htaccess 文件在将根文件夹重定向到文件时返回 404 错误
【发布时间】:2019-04-25 00:56:42
【问题描述】:

我对此束手无策。

我有一个 .htacces 文件,我用它来将某些根目录重定向到文件。

例如:

example.com/example.com/home.php

example.com/subdir/example.com/subdir/home.php

这对我的 subdir 重定向非常有用,但我的根重定向到 home.php 会引发 404 错误,然后将我重定向到我网站的 404 eorr 页面。

这是我的 .htaccess 文件(我把所有东西都留了下来,以防万一另一条规则把东西扔掉了)。

RewriteEngine off
RewriteEngine on
AddDefaultCharset utf-8

#Alter the default time zone for the site
SetEnv TZ Europe/London

#set the RewriteBase
RewriteBase /

#Redirect from root to home.php
RewriteCond %{REQUEST_URI} ^\/?$
RewriteRule (\/)?$ home.php [L]

#Redirect from /subdir to /subdir/home.php
RewriteCond %{REQUEST_URI} ^/subdir\/?$
RewriteRule ^subdir(\/)?$ subdir/home.php [L]

#Removing .php from the end of file paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

#Forcing https://
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI}

#Redirect to 404 error page
ErrorDocument 404 /page_not_found.php
RewriteCond %{HTTP_HOST} ^(www\.)?example.com/(.*)$
RewriteRule ^$ "https://example.com/page_not_found.php" [R=301]

我应该怎么做才能让 example.com 和 example.com/ 重定向到 example.com/home.php?

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    在 cmets 之后试试这个 #Removing .php from the end of file paths 请评论您自己的代码并粘贴。可能对你有帮助

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php/$1 [L]
    

    【讨论】:

    • 感谢您的回复。这让我朝着正确的方向迈出了一步。它现在从example.comexample.com/ 重定向,但是对于https://example.comhttps://example.com 有什么想法吗?
    • 不能再多说了兄弟,但这对我有用,这就是我与你分享的原因:)
    猜你喜欢
    • 2015-08-22
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多