Htaccess: 301 Redirect

Please help me! I have moved my entire site from ASP to PHP and need a 301 Redirect that will send the old asp-page to the new php-page (with the same name - except from the php-extension):

http://www.mysite.com/test.asp --> http://www.mysite.com/test.php

Needs also to work with virtual subdomains:

http://sub.mysite.com/test.asp --> http://sub.mysite.com/test.php 

And default.asp should go to index.php:

http://www.mysite.com/folder/default.asp --> http://www.mysite.com/folder/index.php

This is as fare as I got:

RedirectMatch (.*)default\\.asp$ http://www.mysite.com$1index.php
RedirectMatch (.*)\\.asp$ http://www.mysite.com$1.php

But it is not a 301 redirect and it doesn’t catch subdomains.

Can someone please help me?

Thanks,

Andreasa

Excellent! Thank you very much!

The subdomains are “virtual”. Meaning that the subdomains don’t really exist - you are send to the same page but in the code I show the text (language) that correspond to the “subdomain” in the url. Therefore I hope it would be possible to keep the url in the redirect. Is it possible?

Thanks again!

Rémon,

Aw, come on now! Don’t you expect that andreasa has at least one character in the name of his/her asp files? DON’T use *, use + to require at least one character! Otherwise, :tup:

Regards,

DK

Letters and digits.

In that case you can replace (.) with [a-zA-Z0-9]

[ – start a character class
a-z – any character between ‘a’ and ‘z’ (including)
A-Z – any character between ‘A’ and ‘Z’ (including)
0-9 – any digit between 0 and 9 (including)
] – end of character class

  • – repeat characters from the character class zero or more times

:slight_smile:

Wow, you are fast!

Works great. Thanks again again :slight_smile:

I just want it to keep the same virtual subdomain:

http://sub.mydomain.com/page.asp –> http://sub.mydomain.com/page.php

Instead of as now:

http://sub.mydomain.com/page.asp –> http://www.mydomain.com/page.php

This is as fare as I got:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.html\.net$ [NC]
RedirectMatch permanent (.)default\.asp$ http://%1.html.net$1index.php
RedirectMatch permanent (.
)\.asp$ http://%1.html.net$1.php

But is is not working :frowning:

You can’t mix RewriteCond with RedirectMatch; it won’t work as they are two different systems.
That being said, RedirectMactch cannot help you here at all, since the first part (the from part) is specified using the path only (not the full URL) and in the next part (the to part) you need to use a full URL including schema.
So, using RedirectMatch everything will be redirected to one domain, no option to send it to different domains.

What you’re looking for here is RewriteCond together with RewriteRule:


RewriteEngine On
RewriteRule (.*)default\\.asp$ http://%{HTTP_HOST}$1index.php [L,R=301]
RewriteRule (.*)\\.asp$ http://%{HTTP_HOST}$1.php [L,R=301]

Because of the %{HTTP_HOST} in the right side of those rules it will redirect to the (sub)domain that was requested.

Lastly, please take a look at the article linked in dklynn’s signature, and make those regular expressions more sensible than using the :kaioken:EVERYTHING:kaioken: atom (.*)
Yes - it’s really as bad as I make it out to be!

The syntax for RedirectMatch is as follows:


RedirectMatch [status] regex URL

The status field is optional, and when you leave it out it defaults to 302 - moved temporary. Since you left it out, that’s what you get :slight_smile:

So solve it, put permanent in both lines:


RedirectMatch [COLOR="Red"]permanent [/COLOR](.*)default\\.asp$ http://www.mysite.com$1index.php
RedirectMatch [COLOR="Red"]permanent[/COLOR] (.*)\\.asp$ http://www.mysite.com$1.php

As for the subdomains, these are probably served from a different directory where your .htaccess isn’t applied. Make sure the .htaccess works first and then copy it to the directories where the subdomains are placed.

Lastly, (.*) is not a good way to write regular expressions, since it can match nothing or everything, and can cause of lot of problems when used incorrectly. I’m assuming it works for you, but I’d nonetheless recommend you use something more sensible like [a-zA-Z0-9_-]+ to match only characters, digits, underscores and dashes. Of course the actually regular expression depends on what path names are possible.

What characters can your directory names and files contain? Is it just letters, or letters and digits, or … ?

YES! That was exactly what I was looking for! Thank you so much!

You’re welcome :slight_smile:
Did you change the (.*) for something better? If not, do you want some help with that?

andreasa,

Probably not - BECAUSE each subdomain is treated as a separate domain. Saying that, it must be registered with your local DNS server just to receive the request.

Regards,

DK

I did not. I was just happy that it worked :slight_smile:

If you have a suggestion I would be happy for that as well.