using .htaccess to redirect .asp to .php?

7,866

Solution 1

try adding a slash before the target url ($1) like:

RewriteEngine on
RewriteRule ^(.*)\.asp$ /$1.php [R=301,NC]

Solution 2

Try

RewriteEngine on
RewriteRule ^(.*)\.php$ $1.asp [nc]
Share:
7,866

Related videos on Youtube

PaulHanak
Author by

PaulHanak

Updated on September 18, 2022

Comments

  • PaulHanak
    PaulHanak almost 2 years

    Ahoy all! I've been racking my brain with this one.

    I basically just converted all of my pages from ASP code to PHP code. Kept the same filenames.

    So, here is the htaccess code I have so far, but it keeps redirecting to my ROOT.... ie /home/accountname/publc_html...

    What am I doing wrong?

    RewriteRule (.*)\.asp $1.php [R=301]
    
    • MrWhite
      MrWhite over 11 years
      "Kept the same filenames." - So your PHP files are actually "filename.asp"?
    • Kenzo
      Kenzo over 11 years
      @w3d That's obviously not what he meant. Same file name, new extension.
    • PaulHanak
      PaulHanak over 11 years
      Sorry for the late reply. Yes, I kept the filenames but changed the extensions.
  • PaulHanak
    PaulHanak over 11 years
    Thanks for the reply. Hmmm... that actually didn't do anything. It isn't redirecting at all.
  • bekay
    bekay over 11 years
    What web server are you using?
  • PaulHanak
    PaulHanak over 11 years
    Sorry for the late reply. Appreciate the help! Linux. I tried with both the NC and the R=301. With the 301, it seems to redirect to the ROOT. For example, it will redirect from sitename.com/name.asp to sitename.com/home/accountname/name.php. Weird!
  • bekay
    bekay over 11 years
    @PaulHanak is there any other rewrites in your .htaccess would you please paste the entire file if so.
  • PaulHanak
    PaulHanak over 11 years
    Thank you for the follow-up. The only other rewrite in the htaccess is for hidding hidden files and directories as per the boiler plate. RewriteCond %{SCRIPT_FILENAME} -d RewriteCond %{SCRIPT_FILENAME} -f RewriteRule "(^|/)\." - [F] I have also placed your code at the very top of my htaccess file if that makes a difference.
  • WebChemist
    WebChemist over 11 years
    in Apache you can make .asp work as .php with an AddHandler rule like AddHandler application/x-httpd-php .php .asp (or /x-httpd-php5 depending on how the php handler is defined)
  • PaulHanak
    PaulHanak over 11 years
    Well, this was it, except backward!! RewriteRule ^(.*)\.asp$ /$1.php [R=301]
  • WebChemist
    WebChemist over 11 years
    hah thanks for pointing that out, not sure how I missed that one. I'll edit the answer