-2

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  
Piyush
  • 1
  • 4
  • Question is not much clear, i am trying to understand your requirement but there is possibility that i may understand it wrong . Please elaborate your question to get quick and appropriate solutions. – imvikasmunjal Jan 09 '18 at 11:29
  • i have some old html url and i want to redirect it to new php url – Piyush Jan 09 '18 at 11:36

1 Answers1

0

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. (301s are cached hard by the browser, so can make testing problematic.)

MrWhite
  • 11,643
  • 4
  • 25
  • 40