Forum Moderators: coopster

Message Too Old, No Replies

Redirecting extensions asp to php

It adds DocumentRoot into path

         

dstiles

10:41 am on Jul 7, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need to redirect ASP files to PHP extension after moving away from a Windows server. Easy. Check online for info, implement, swear.

I have tried two methods, the first recommended on the apache site, the second an arbitrary "tips" site with a variation of my own.
# backward compatibility ruleset: rewrite doc.asp to doc.php if doc.php exists
# <Directory "/var/srv/examplesite">
RewriteEngine on
RewriteBase "/srv/examplesite"
RewriteCond "$1.php" -f
RewriteCond "$1.asp" !-f
RewriteRule "^(.*).asp$" "$1.php"
# </Directory>
RewriteRule ^([a-z0-9\-/]+)\.asp$ $1\.php [R=301,L]
OR
RewriteRule ^(.*)\.asp$ $1\.php [R=301,L]

I have tried the first in the config file as part of an existing <directory> and in it's own <directory> and tried both in htaccess. In all cases I get https://www.example.co.uk/srv/examplesite/xxx.php. I have tried variations of RewriteBase but it always ends up with the DocumentRoot value or a 500.

It's almost working but annoyingly not correct. Help, please?

brotherhood of LAN

11:58 am on Jul 7, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a suggestion rather than answering your question, why not just let PHP deal with .asp pages? It makes no difference to the client side and it'll spare you having to redirect everything.

dstiles

12:10 pm on Jul 7, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't want asp-extension files on the server. I block asp extension accesses on all other sites on the server and return Gone for bots for most of them.

w3dk

12:38 pm on Jul 7, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



... I block asp extension accesses on all other sites


Ah, I was going to ask whether you wanted to keep the ".asp" in the URL - which is the intention of the first rule block (but the RewriteBase directive is likely triggering a 302 redirect).

You should also avoid the filesystem checks - that first rule block is intended to be used when in the middle of changing a site from one to the other - and maintaining the URL structure.

Note also that mod_rewrite directives in .htaccess will override mod_rewrite in the <Directory> container by default.

RewriteRule ^(.*)\.asp$ $1\.php [R=301,L]


If used in .htaccess or a Directory container (in the server config) then you are missing a slash prefix on the substitution string - this will otherwise expose the document root.

In other words:

RewriteRule (.+)\.asp$ /$1\.php [R=302,L]


This could also be resolved by setting "RewriteBase /" - but simply prefixing the slash on the rule is preferable.

Clear your browser cache and test with 302s first (to avoid caching issues).

If your asp URLs following a specific format then use a more restrictive regex, as in the earlier RewriteRule.

dstiles

3:40 pm on Jul 7, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



intended to be used when in the middle of changing a site from one to the other - and maintaining the URL structure.

That is my intention. I have migrated the whole web site from ASP to PHP, file-for-file.

Adding that / fixed the problem, which I'm sure I did before but forgot to clear the cache. :(

Many thanks! :)

w3dk

4:50 pm on Jul 7, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



RewriteRule (.+)\.asp$ /$1\.php [R=302,L]


Minor point... no need to backslash escape the literal dot in the substitution string (2nd argument) - the dot carries no special meaning here (it's not a regex). ie. "/$1\.php" can be written "/$1.php" - the result is exactly the same.

lucy24

5:36 pm on Jul 7, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Final note: R=302 is redundant, since 302 is the default with the [R] flag; it's R=301 that has to be stated explicitly. Granted, in some cases you may want to keep it just as a reminder to yourself that this particular rule is intended to be temporary.

dstiles

9:42 am on Jul 8, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



w3dk - thanks for that. I'm never sure when to include an escape.

lucy - mine is a 301 anyway.

ravikumarfront

11:51 am on Jul 8, 2020 (gmt 0)

5+ Year Member



Hi Dstiles,

Please take the following rule code as a reference.

<rule name="redirect asp to php" stopProcessing="true">
<match url="^site/(.*).asp" />
<action type="Redirect" url="/site/{R:1}.php" />
</rule>
Best Regards,

Ravi Kumar

ravikumarfront

11:51 am on Jul 8, 2020 (gmt 0)

5+ Year Member



Hi Dstiles,

Please take the following rule code as a reference.

<rule name="redirect asp to php" stopProcessing="true">
<match url="^site/(.*).asp" />
<action type="Redirect" url="/site/{R:1}.php" />
</rule>

Best Regards,

Ravi Kumar

dstiles

8:50 am on Jul 9, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ravi thanks, but that looks like Windows server code. I'm sure it does the same job but I'm working here on apache.