Homemade URL shortener redirect conflicting with 301 redirects

Homemade URL shortener redirect conflicting with 301 redirects - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Homemade URL shortener redirect conflicting with 301 redirects error on your web browser. Problem :


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?


Solution :

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]


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


We hope that this article has helped you resolve the htaccess, redirects, error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

Redirected urls show in serp, with the original permalink but with the title and meta of the target page

How can I redirect everything but the index as 410?

How do I redirect traffic only if being accessed from a specific port?