Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a website which was originally built using classic ASP. About a year ago it was rewritten in PHP. This caused file extensions to change and some pages were moved to new locations within the website. Naturally we made 301 redirects from the old URLs to the new URLs so their users and search engines could find their new locations. This works fine.

We also built them a tool that allows the site owner to create their own tiny URLs that will redirect to a page with Google Analytics campaign tracking variables included in the URL so they can track the campaign and see how it performs.

The issue I have is the campaign tracking rules conflicts with the 301 redirects and prevents the redirects from working at all.

Here is the campaign tracking code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /tracking.php?$1 [R=301,L]

As you can see it checks to see if a file exists and if it doesn't it redirects to a tracking script (tracking.php) which then takes the tracking code and redirects to the proper URL with the Google Analytics campaign code in the query string.

That code causes rules like this to fail:

redirect 301 /about.asp /about.php
redirect 301 /capabilities.asp /capabilities.php
redirect 301 /capacitors.asp /capacitors.php

It makes sense that this doesn't work together as the tracking rules say, "if a file isn't found redirect to tracking.php". What I need is for the 301 redirects to run first, and if none of them match, then redirect to the tracking script. Placing the tracking rules below the 301 redirects does not change the results.

Can this be done via .htaccess? Or will I need to modify the tracking script so that if a campaign is not found it checks to see if the campaign ID is actually an old page that needs to be redirected and then do the 301 redirect from there?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Try converting your mod_alias redirects into mod_rewrite directives.


Edit: Example below assumes that you have replaced old ASP files with PHP files under the same path.

RewriteEngine on

# *.asp -> *.php
RewriteRule (.*)\.asp$ /$1.php [R=301,L]

# tracking script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /tracking.php?$1 [R=301,L]

... or you can skip the regular expression variable and set an explicit redirect for each of the old URI's:

RewriteRule about\.asp$ /about.php [R=301,L]
RewriteRule capabilities\.asp$ /capabilities.php [R=301,L]
RewriteRule capacitors\.asp$ /capacitors.php [R=301,L]

Edit #2: Here is an example which should cover most of the situations you'll encounter:

RewriteEngine on

# Changes to path + Query String Append
RewriteRule ^/path/to/old\.asp$ /path/to/new/about.php [R=301,L,QSA]

# *.asp -> *.php + Query String Append
RewriteRule ^(.*)\.asp$ $1.php [R=301,L,QSA]

# tracking script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /tracking.php?$1 [R=301,L]
share|improve this answer
    
I'm not sure what you mean. Can you give an example based on what I've shown in my question? –  John Conde Mar 29 '11 at 23:01
    
Thanks for the edit. Makes sense now. I guess my last issue would be what to do with the files whose paths have changed? Is that possible? Or am I asking too much? –  John Conde Mar 30 '11 at 14:24
    
You can set explicit redirects for those files (if most of the paths have changed, you may as well use explicit redirects for all the files to ensure the configuration is maintainable). –  danlefree Mar 30 '11 at 14:29
    
I suspect not having the [R=301,L] after each redirect is what's causing my issue. Let me try that and I'll let you know what happens. –  John Conde Mar 30 '11 at 14:31
    
The *.asp -> *.php wildcard works great. The individual rules for the pages that aren't a direct transition and have query strings are being troublesome. But this seems like the right way to go. If I can't get that part sorted out I'll start a new question about it. –  John Conde Mar 31 '11 at 14:06

I would reverse the redirect and leave it as it originally was. Redirect your NEW site to your OLD site. Your OLD site probably is already benefiting from back-linking, age of website, and SEO. It would be a shame to lose that just to replace it with a Newer site w/ a lower page rank. Google punishes redirects. Your NEW site won't have any SEO on it yet so it's better to use a redirect on that one :)

share|improve this answer
    
I don't think regressing is a good solution. The original site was poorly done in just about every way. We made the site faster, more usable, accessible, organized, and maintainable. They won't lose anything from the redirects because it's a niche site and had few links to begin with and most went to the home page which doesn't require a redirect. So we definitely don't want to go back to the old site at all as we gain nothing and lose a lot. –  John Conde Mar 29 '11 at 17:18
    
FYI, Google doesn't punish for redirects. The only issue redirects have is if they're chained too deeply they stop following them and some PR is lost when doing a 301 but the amount is trivial and worth it compared to the alternatives of not doing it at all. –  John Conde Mar 29 '11 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.