1

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.

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]

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

6 Comments

Hello Anubhava, previously you helped me for rewrite url using htaccess and its working perfect but now in the same project I need the same rule I mean I just need to replace .php with .html extension. Like previously you helped me to rewrite rule for two parameters and three but now there is no parameter only file with php extension. Your provided solution doesn't help it redirects to me to localhost/lions/.html with no file found server error
I've updated my question, please check its redirecting to correct url but it got server error requested url not found and also my other rules are not working either
I think rewrite base is creating problem when I go to localhost/lions/contacts.php then it redirects to localhost/lions/lions/contact.html and if I remove rewrite base then contact url works but not other url
Yeah its working fine thanks a lot, you are really a genius in regx and htaccess
Hello Anubhava, Please look into this question stackoverflow.com/questions/29508079/… I am sure you can help me in this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.