Forum Moderators: phranque

Message Too Old, No Replies

Help 301 Redirect old asp site to PHP

Help 301 Redirect old asp site to PHP

         

russwittmann

9:29 pm on Mar 21, 2009 (gmt 0)

10+ Year Member



Hello,

I have an old site that was recently converted to a php (expression engine cms site) all the old content is gone and I need to redirect the pages to the proper place except i'm having issues with my 301 redirect...

the old url is [domain.com...]

the new url is [domain.com...]

the redirect method I tried was

Redirect 301 /contentpage.asp?ContentID=7 [domain.com...]

but that is not working and im assuming it has something to do with the parametrized url (?ContentID=7) I would appreciate if anyone could help out here and would be greatly appreciative.

Thank You
Russ

g1smd

9:38 pm on Mar 21, 2009 (gmt 0)

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



You can't use Redirect or RedirectMatch for this.

Use a RewriteCond to look at QUERY_STRING, and a RewriteRule with [R=301,L] for the redirect.

Include the full canonical domain name in the target URL.

russwittmann

11:02 pm on Mar 21, 2009 (gmt 0)

10+ Year Member



I tried something like this but I'm lost.... because its not working either

RewriteCond %{QUERY_STRING} ^ContentID=7$
RewriteRule ^contentpage\.asp$ [domain.com...] [R=301,L]

g1smd

11:17 pm on Mar 21, 2009 (gmt 0)

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



That code allows for the
ContentID=7
parameter to be the ONLY parameter present. Is it?

I would code that as:

&?ContentID=7&?
or similar, so that it would still redirect if other parameters were present.

Other than that, I see no errors or issues.

Did you flush the browser cache before testing?

jdMorgan

11:29 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To prevent the original query string from being appended to the new URL, add a question mark to the substitution URL:

RewriteCond %{QUERY_STRING} &?ContentID=7&?
RewriteRule ^contentpage\.asp$ http://www.example.com/abou[b]t/?[/b] [R=301,L]

We're presuming that you already have other working rewriterules in this file. If not, you'll need to add either the second line or both of these lines ahead of the code above:

Options +FollowSymLinks
RewriteEngine on

The first line is not required on all servers. In fact, it's not allowed on some servers. The only way to find out whether both lines are required is to test.

Jim

russwittmann

11:39 pm on Mar 21, 2009 (gmt 0)

10+ Year Member



ok this is working but it conflicts with my other rewrite rules here is what I have

[old rules and I need these]

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
[end old rules] ------expression engine cms uses these------

[ redirect rules ] ----works when i remove above rules -------
RewriteCond %{QUERY_STRING} &?ContentID=7&?
RewriteRule ^contentpage\.asp$ http://www.example.com/about/? [R=301,L]

russwittmann

11:56 pm on Mar 21, 2009 (gmt 0)

10+ Year Member



Fixed it by placing the new rules before like so, please let me know if this is correct because I will be writing about 15 of these.... thank you

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{QUERY_STRING} &?ContentID=7&?
RewriteRule ^contentpage\.asp$ http://www.example.com/about/? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

jdMorgan

12:15 am on Mar 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



External redirects should always precede internal rewrites, with rules in both the redirect and rewrite groups in order from most-specific (fewest URLs affected) to least-specific.

Jim

russwittmann

12:58 am on Mar 22, 2009 (gmt 0)

10+ Year Member



was my fix correct? it seems to be working correctly :)

g1smd

8:46 am on Mar 22, 2009 (gmt 0)

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



It looks fine to me.

However, I would also add a couple more rules.

After the first redirect I would add a rule which fixes URL requests asking for an index file by name to redirect to URL at www with trailing slash and no index file name.

After that I would have the general non-www to www redirect that fixes all non-www requests and preserves the requested filepath when redirecting to www.

After that I would list the rewrite.

russwittmann

6:53 pm on Mar 22, 2009 (gmt 0)

10+ Year Member



I'm so new to mod rewrite how would I do that? I'm really interested.

g1smd

6:55 pm on Mar 22, 2009 (gmt 0)

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



Luckily. Examples for both of those are posted several times each week.

russwittmann

9:32 pm on Mar 22, 2009 (gmt 0)

10+ Year Member



got it but which method is better I found two below?

RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9\-]*)\.com$
RewriteRule ^.*$ [%{HTTP_HOST}%{REQUEST_URI}...] [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

g1smd

9:52 pm on Mar 22, 2009 (gmt 0)

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



Try this one for the non-www to www rule:

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

You also need the 'index file to slash' redirect placed directly ahead of this one. That redirect also needs to force www for those requests.

russwittmann

11:35 pm on Mar 22, 2009 (gmt 0)

10+ Year Member



g1smd are you talking about this rule?

RewriteRule ^(.*)$ /index.php/$1 [L]

g1smd

11:40 pm on Mar 22, 2009 (gmt 0)

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



Nope. That is your rewrite. Don't change that, but do list it last.

Ahead of the non-www to www redirect you need a separate 301 redirect that strips "index" filenames from the requested URL, and forces www for those requests.

russwittmann

1:53 am on Mar 23, 2009 (gmt 0)

10+ Year Member



im in a whirl wind of information overload....

people can get to my page via [domain.com...] or [domain.com...] somone said I need to force the ar of the backslash which I am trying like this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}¦/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

but this isnt working and Im going seriously bald...lol

g1smd

8:43 am on Mar 23, 2009 (gmt 0)

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



Personally, I would remove the slash, as a slash is supposed to represent a folder.

You do that with a redirect, but again, a redirect must contain the full domain name in the target URL otherwise that redirect creates a non-www and www duplicate URL, which then gets corrected by a later redirect creating a redirection chain. Fix the www part within the same redirect for these requests.

There's plenty of prior example code for removing the slash.