How to redirect old html page to new php page. I used this code but it redirecting with full file path. I'm using below code.
RewriteEngine On
RewriteRule ^(.*).html$ $1.php
How to redirect old html page to new php page. I used this code but it redirecting with full file path. I'm using below code.
RewriteEngine On
RewriteRule ^(.*).html$ $1.php
but it redirecting with full file path
It will use the "full file path" if you specify a relative substitution. ie. a substitution that does not start with a slash (/
) or scheme+host (absolute URL). In your example, using .htaccess
, the captured backreference excludes the slash prefix on the URL-path. So, you need to explicitly include a slash prefix on the substitution. For example:
RewriteRule ^(.*).html$ /$1.php
However, if you need an external redirect (as you are describing in your question) then you will also need to explicitly include the R
(redirect
) flag on the RewriteRule
, otherwise you'll get an internal rewrite (the URL will not change - but your site may still work with the old .html
URL).
And if you have other directives in your file, you may also need the L
(last
) flag to prevent further rewrites. For example:
RewriteRule ^(.*).html$ /$1.php [R=302,L]
If this should be a redirect (as opposed to a rewrite) and this is a permanent change to your URL structure then change the 302
(temporary) to 301
(permanent) only when you are sure it's working OK. (301
s are cached hard by the browser, so can make testing problematic.)