Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 & Rewrite, need guide to avoid loops

         

designerRT

7:14 pm on Jul 26, 2007 (gmt 0)

10+ Year Member




Hello guys, I'm switching from HTML+ASP over PHP and I need a little help to know if I'm on the right track, or my code is a total mess. BTW, please excuse if my English is a bit rusty ;)

My case:

I have a simple web structure:

www.mydomain.com/page1.htm
www.mydomain.com/page2.htm
www.mydomain.com/page3.htm
www.mydomain.com/pageX.asp
www.mydomain.com/pageZ.asp

And Im switching over a more complex PHP:

www.mydomain.com/folder/page1.php?pID=1
www.mydomain.com/folder/page7.php?pID=4
www.mydomain.com/folder/page2.php?pID=3
www.mydomain.com/folder/page10.php?pID=15
www.mydomain.com/folder/page1.php?pID=4

with several more pages with the same above structure and:

www.mydomain.com/folder/page.php?pID=5&uID=14

My intentions:

1. Want to redirect the basic old structure to the equivalent ones (with constant url) on the new php web. Reason: to keep my PageRank and don't want to worry about the backlinks.
2. Want to rewrite my urls to be more Search Engines/Robots/Spiders friendly...maybe more human friendly also ;).
3. Also I want to have:
a) just few landing pages with a more descriptive url,
b) the rest with a general url, for example several "www.mydomain.com/folder/page.php?pID=5&uID=14" with more random uID values.

My work:

redirect 301 /page1.htm folder/page1.php?pID=1
redirect 301 /page2.htm folder/page7.php?pID=4
redirect 301 whatever/ folder/page1.php?pID=1
redirect 301 folder/([a-z]+)([0-9]+)\.php\?pID\=([0-9]+) www.mydomain.com
redirect 301 folder/([a-z]+)([0-9]+)\.php\?pID\=([0-9]+)\&uID\=([0-9]+) www.mydomain.com

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^folder/page1.php?pID=1$ whatever/ [R=301,L]
RewriteRule ^folder/page7.php?pID=4$ whoever/ [R=301,L]
RewriteRule ^folder/([a-z]+)([0-9]+)\.php\?pID\=([0-9]+)$ example/ [R=301,L]
RewriteRule ^folder/([a-z]+)([0-9]+)\.php\?pID\=([0-9]+)\&uID\=([0-9]+)$ example/ [R=301,L]

...The first two redirects are kinda obvious, the next two redirects are something I thought I should have in case someone bookmark the RewriteRule urls that result from the next section of code.

So if I understand it, the first RewriteRule should write the url "www.mydomain.com/whatever/", and same thing works for the second one. The third one should work for the rest of pages not considered by the previous 2 rules, and again same thing goes for the last one.

I can't test it right now, because my hosting still needs to set up the new Linux server, but I want to be ready.

Thank you in advance.

jdMorgan

7:56 pm on Jul 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suggest that you do not use external redirects. Use internal rewrites so that you keep the old URLs, even though the associated filenames may change and your scripts may require query strings:

# Rewrite Old URL-paths to new files or scripts
RewriteRule ^page1\.htm$ /folder/page1.php?pID=1 [L]

To avoid duplicate content problems, you may need to redirect any HTTP requests for the new URL-path back to the old URL. This shows the code needed to avoid a loop with the rule above:

# Redirect client requests for new URL-paths back to old URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/page1\.php\?pID=1\ HTTP/
RewriteRule ^folder\.page1\.php$ http://www.example.com/page1.html [R=301,L]

This approach allows you to change your files without any indication to users or to search engines that your site has changed in any way.

Jim

ogletree

8:04 pm on Jul 26, 2007 (gmt 0)

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



You don't have to do all that. Just run asp pages through the php engine.

AddType application/x-httpd-php .asp

This will help you not lose your place in the search engines and it will add a level of security. People won't try to hack your code.

If you have to switch I would not go with the ugly URL's. Don't use question marks, periods, ampersands, or equal signs.

Read this post [webmasterworld.com].

designerRT

8:37 pm on Jul 26, 2007 (gmt 0)

10+ Year Member



@ jdMorgan

Thanks, even thought I believe that is more information that I can handle. Anyway, I will try to learn it and test it before asking again.

@ ogletree

The asp code we have now is a total mess, full of bugs.
Sadly this proyect started before I could put my eyes on it. I know those urls are ugly, but asking the actual developer to rewrite the whole system to avoid them it's impossible right now. That is why I must learn about the Rewrite rules fast, in order to minimize the problems. Wish me luck.

ogletree

9:52 pm on Jul 26, 2007 (gmt 0)

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



I'm not talking about the code. I'm talking about the htaccess. Instead of redirecting just rewrite the url's. Since you have html as well you will have to put

AddType application/x-httpd-php .html
AddType application/x-httpd-php .asp

Then instead of the redirect you rewrite the url. There will be a little work for the developer to make the site link internally to the right url's but that should not be a big deal. What you are wanting to do should be the last thing you try when nothing else works. If you do this your site is going to have lots of problems with search engines for a while. Make sure your company knows that doing this will lose them traffic for a while.

g1smd

9:53 pm on Jul 26, 2007 (gmt 0)

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



Just to be clear what a redirect and a rewrite are all about.

A redirect sends "301", and a new URL for the content, back to the browser and the browser then requests the new URL in a new transaction. The server then delivers that content. Search engines will index the new URL too.

A rewrite takes the requested URL and translates it into an internal filename. The server fetches that internal file and delivers the content back to the browser without the browser seeing what that filepath actually is.