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]
Related videos on Youtube
Author by
PaulHanak
Updated on September 18, 2022Comments
-
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 over 11 years"Kept the same filenames." - So your PHP files are actually "filename.asp"?
-
Kenzo over 11 years@w3d That's obviously not what he meant. Same file name, new extension.
-
PaulHanak over 11 yearsSorry for the late reply. Yes, I kept the filenames but changed the extensions.
-
-
PaulHanak over 11 yearsThanks for the reply. Hmmm... that actually didn't do anything. It isn't redirecting at all.
-
bekay over 11 yearsWhat web server are you using?
-
PaulHanak over 11 yearsSorry 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 over 11 years@PaulHanak is there any other rewrites in your .htaccess would you please paste the entire file if so.
-
PaulHanak over 11 yearsThank 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 over 11 yearsin 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 over 11 yearsWell, this was it, except backward!! RewriteRule ^(.*)\.asp$ /$1.php [R=301]
-
WebChemist over 11 yearshah thanks for pointing that out, not sure how I missed that one. I'll edit the answer