.htaccess htaccess重定向和从php重写为html

.htaccess htaccess重定向和从php重写为html,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我被.htaccess修改卡住了,需要一些帮助。 首先,这是我的htaccess中的内容 RewriteEngine on RewriteRule ^(.*)\.html$ $1.php [nc] 例如,我创建了一个名为test.php的文件,并上传到我的服务器 我希望我的服务器能像这样工作 http://example.com/test/test.html -> http://example.com/test/test.html(as it is) http://example.com

我被.htaccess修改卡住了,需要一些帮助。
首先,这是我的htaccess中的内容

RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
例如,我创建了一个名为test.php的文件,并上传到我的服务器

我希望我的服务器能像这样工作

http://example.com/test/test.html -> http://example.com/test/test.html(as it is)
http://example.com/test/test.php -> http://example.com/test/test.html
但我现在有了.htaccess, 我仍然有.php和.html,这可能被谷歌机器人(GoogleRobot)等搜索引擎爬虫视为文件复制(不是吗?)


感谢您的帮助。

试试这个.htaccess代码

# Enable Rewrite Engine
RewriteEngine on

#Create friendly URL
RewriteRule ^test/(.*)\.html$ test/$1\.php [L]

RewriteCond %{REQUEST_URI} ^(.*)\.php$
RewriteRule ^(.*) /$1.html [L]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## .php to .html
# To externally redirect /test/test.php to /test/test.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1.html [R,L,NC]

RewriteCond %{REQUEST_URI} ^(.*)\.php$
RewriteRule ^(.*) /$1.html [L]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## .php to .html
# To externally redirect /test/test.php to /test/test.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1.html [R,L,NC]

这是apache“.htaccess”的配置文件,如果仍要配置,请更改

重新启动发动机


我希望这项工作

您可以在根目录中的一个.htaccess文件中尝试:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.html             [NC]
RewriteRule ^([^/]+)/([^.]+)\.php  /$1/$2.html [R=301,NC,L]

对于静默映射,请将[R=301,NC,L]替换为[NC,L]

感谢Sumit的快速回复。但是我的文件名可以是任何东西。像index.php、company.php、mission.php、access.php等等。。。所以最好使用正则表达式,对吗?如果不起作用,请检查我的
部分答案,这将是挑战性的work@norixxx您有
test
文件夹,所以规则可能是
RewriteCond%{REQUEST\u URI}^test/(.*)\.php$RewriteRule^test/(.*))/test/$1.html[L]
仍然不工作在“/test/index.php->正常。但是如果我键入/test/index.html->“在这台服务器上找不到请求的URL/test/index.html。”我的服务器说。我的htaccess现在:RewriteCond%{REQUEST\u URI}^test/(.*)\。php$RewriteRule^test/(.*)/test/$1.html[L]太好了!很高兴帮助你:)谢谢。我在htaccess的第一行写了‘RewriteEngine on’。但仍然不起作用。