Mobile app version of vmapp.org
Login or Join
Pope3001725

: Homemade URL shortener redirect conflicting with 301 redirects I have a website which was originally built using classic ASP. About a year ago it was rewritten in PHP. This caused file extensions

@Pope3001725

Posted in: #Htaccess #Redirects

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? [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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Pope3001725

2 Comments

Sorted by latest first Latest Oldest Best

 

@Cody1181609

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$ /.php [R=301,L]

# tracking script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /tracking.php? [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$ .php [R=301,L,QSA]

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

10% popularity Vote Up Vote Down


 

@Gretchen104

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 :)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme