需要使用htaccess将php页面重定向到html

问题描述:

我需要将.php页面重定向到.html。我的网址就像 http://localhost/lions/contacts.php ,我需要像 http:// localhost / lions / contacts一样重写。 html 使用htaccess。

I need to redirect .php page to .html. My url is like http://localhost/lions/contacts.php and I need to rewrite like http://localhost/lions/contacts.html using htaccess.

直到现在我使用以下代码:

Till Now I've used following code:

RewriteRule . %1.html [R=301,L]
RewriteRule ^([^./]+)\.html$ index.php

但是它不起作用。请帮忙谢谢。

But its not working. Please help thanks.

我的完整htaccess文件如下

My Complete htaccess file looks like

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /lions/

#redirect localhost/lions/page.php?page_id=1&album_id=1&action=contacts to localhost/lions/1/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]

#redirect localhost/lions/page.php?page_id=1&action=contacts to localhost/lions/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

#redirect localhost/lions/contacts.php to localhost/lions/contacts.html

RewriteCond %{THE_REQUEST} /(.+?)\.php[\s?] [NC]
RewriteRule ^ %1.html [R=302,L,NE]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)\.html$ $1.php [L]


您可以在 /lions/.htaccess 文件中使用以下代码:

You can use this code in your /lions/.htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /lions/

#redirect localhost/lions/page.php?page_id=1&album_id=1&action=contacts to localhost/lions/1/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . /%1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]

#redirect localhost/lions/page.php?page_id=1&action=contacts to localhost/lions/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . /%1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

#redirect localhost/lions/contacts.php to localhost/lions/contacts.html

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]

RewriteRule ^(.+?)\.html$ $1.php [L,NC]