.htaccess Apache localhost找不到文件,除非它们是索引文件

uxhixvfz  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(149)

得到了一个新的个人电脑,并试图设置我的环境一样,我以前的环境。

根索引加载主页(index.php)

本地主机/根目录/
404页面未找到404页面未找到
本地主机/root/联系人-用户

contact-us.php如果我添加扩展名.php则会找到该文件

localhost/root/contact-us.php

其他index.php文件也可以使用,例如

本地主机/根目录/博客/本地主机/根目录/产品/
例如根〉博客〉索引
但是找不到根〉blog〉this-new-blog。
一切都在网站的现场版本工作,只是不能让我的本地主机工作...

RewriteEngine On

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# Redirect to www.
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

# Redirect HTTP to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]

# Error redirects
ErrorDocument 400 https://www.mywebsite.com/error/bad-request
ErrorDocument 401 https://www.mywebsite.com/error/unauthorised
ErrorDocument 403 https://www.mywebsite.com/error/forbidden
ErrorDocument 404 https://www.mywebsite.com/error/file-not-found
ErrorDocument 500 https://www.mywebsite.com/error/internal-server-error

我在我的.htaccess中有上面的代码,用于删除.php,它以前在我的旧PC上修复了这个问题。
有没有什么想法,我需要做什么才能让这个工作?

zmeyuzjn

zmeyuzjn1#

不知道为什么,但这个更新的htaccess代码修复了我的问题。

############ LIVE ONLY ############
#
# force WWW
# RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
# RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

# force HTTP to HTTPS
# RewriteCond %{SERVER_PORT} 80
# RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
#
############ LIVE ONLY ############

RewriteEngine on
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /dir/file.php to /dir/file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# Error redirects
ErrorDocument 400 https://www.mywebsite.com/error/bad-request
ErrorDocument 401 https://www.mywebsite.com/error/unauthorised
ErrorDocument 403 https://www.mywebsite.com/error/forbidden
ErrorDocument 404 https://www.mywebsite.com/error/file-not-found
ErrorDocument 500 https://www.mywebsite.com/error/internal-server-error

我添加了删除尾部斜杠代码,并注解掉了在本地工作时强制执行www和强制执行https请求。
现在我可以查看我所有的网页与干净的网址。

相关问题