.htaccess - error404 redirect within a directory?
-
Hi,
One of my clients has a CMS website offering Health and Safety training. When the courses have been run they automatically drop off of the system which is great for the front-end of the site but this leaves pile 404 errors for the URLs.
I am trying to put a .htaccess redirect in place that will redirect back to the main category for that course i/e :
http://www.domain.co.uk/courses/highways/6-NRSWA/27-nrswa-operative-sept-11.html
will redirect to
http://www.domain.co.uk/courses/highways/6-NRSWA
I have spent a looooong time hitting google for a solution but can't seem to come up with anything.
If at all possible I would also like to be able to post a php variable via the redirect url so that I can display a message on the category page saying that the course is no longer available be please select a different course. i/e:
http://www.domain.co.uk/courses/highways/6-NRSWA?course=not-available
Any help on this would be most gratefully received.
-
Thanks again for your input Sha.
I got there in the end!
-
I am not one to settle for a solution that I am not entirely happy with and although creating a 301 redirect to the home page of the site for courses that have dropped off of the system is fine for SEO, it doesn't help the site user that can't find the relevant course details.
Rather than a redirect solution using .htaccess, which would have been way easier had Joomla allowed it, I have resorted to adding some php within the main Joomla error.php file. Here's the code :-
// check if joomla http status code is 404 error
if ($this->error->code == '404') {// get current page URL
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}// check to see if 404 error relates to courses
if (strstr(curPageURL(), 'courses'))
{// strip last part of URL so that new url relates to relevant category
$newurl = dirname(curPageURL());// add query to url to trigger pop-up course not found message after page is redirected
$redirect = $newurl . '?course=notfound';// 301 redirect to relevant course category
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: " . $redirect);
exit();
}
}
?> -
Hi Ade,
My apologies on this one. My brain was a little addled after a long day of driving I think!
You are correct in that the "set and forget" solution we gave you to start with will be overwritten by the Joomla error handling after the .htaccess file has been read.
It is only possible to make standard 301's work using the .htaccess file if you were to manually create an individual rule for every deleted file using its specific joomla generated URL like this:
RewriteEngine On
RewriteRule ^courses/highways/6-NRSWA/27-nrswa-operative-sept-11.html$ /courses/highways/6-NRSWA?course=not-available [R=301,L]
The problem with this is that if your courses are regularly deleted, it will not be long before you have a very large .htaccess file, which could foreseeably lead to processing issues.
So in fact, the only "set and forget" solution is the one that you have already put in place and from a practical standpoint, it is the best solution.
Well done!
Sha
-
Hi Ade,
We have used .htaccess to create 301 redirects for Joomla sites in the past.
Can you email me your .htaccess file and the URL of your site and I will get our Chief Programmer to take a look for you.
My direct email is on my profile page (or you can private message me from your profile).
Sha
-
Hi Sha,
Thanks again for your time in helping me out.
Unfortunately with Joomla your suggestion doesn't work. I think what Joomla does is to check to see if a component or article exists and if it doesn't then it redirects to a file called error.php.
So essentially there is no 404 error for the .htaccess file to act upon as a page has been found that matches the URL, it just happens to be a very un-SEO friendly page churned out by Joomla.
I have tried adding - ErrorDocument 404 index.php - to the very top of the .htaccees file but this also does nothing.
It looks like joomla does not allow any true 404 errors to occur : (
I am sure that there must be some way of disabling the joomla error handling to allow the .htaccess file to handle them instead but I haven't found it yet. I have found a lot of other people asking the same question but no solution.
Thanks again.
Ade.
-
Hi Ade,
So sorry I wasn't around to follow this up for you. I have been away for the day and had wireless connection issues, so could not check Q&A until now.
Oops! Yes. Joomla does have its own error handling which does make a big difference, but it should be simple to fix once you understand what happens when you put the .htacces file in place.
When a request is received by the server, the .htaccess file is read from top to bottom, checking each rule in the file for a match. Once a match is found, the specific action assigned to that rule is executed. This means that no rules thereafter are read.
So, if you ensure that your code appears at the beginning of the .htaccess file, then whenever the conditions described by the rule are matched, the redirect will occur. However, if no other rule in the .htaccess is matched, then Joomla error handling will come into play should any other error be present.
This of course means that any specific rule you wish to add in the future should also appear before the Joomla code. As long as you always make sure it is last to be read, everything should work just as you intended.
Hope this helps,
Sha
-
Just as an update for anyone else trying to resolvee tis same issue on Joomla 1.5;
I haven't managed to find a way of disabling the Joomla error handling that is over ruling the .htaccess redirects so what I have done as a temporary measure is to modify the Joomla error 404 page so that it re-directs back to the home page of the site.
If I do find a solution I will update this thread.
-
Hi Sha,
Thank you for taking the time to help me out, I really appreciate it.
I have just spent the last few hours scratching my head and googling as I had already tried your solution but it didn't work for me. I did think that I must have made some sort of typo and so I copied and pasted from your code which works fine in your examples but again, it doesn't work on my site.
I think that I have found the reason and also discovered that I may have opened a bigger can of worms than I thought. I maybe should have said in my original post that this site was a Joomla 1.5 site but I didn't think that it would make any difference as the .htaccess file is outside of the joomla installation.
Apparantly Joomla overwrites any HTTP Status errors and so any .htaccess rewrites for files not found won't be carried out.
I think that my best option is to try to find out if it is possible to disable the Joomla HTTP Status error handling and to use your solution but unfortunately it's going to have to wait until tomorrow as I am already in the bad books for working late again.
If I find a solution I will post it here.
My issue is probably still a long way off being resolved but you answered my question perfectly and I am very grateful for your help.
-
Hi Ade,
Since learning this one thing will be something that you are likely to use over and over, I figure it is much better if you see how it actually works. So, we wrote a little resource to show you how to do a basic 301 redirect as well as one that goes back one level to your category page.
If you take a look at this simple 301 Redirect course for managing 404 errors, you can see three working pages and also download the code.
Let me know if you have any questions.
Hope that helps,
Sha
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Should I redirect 404s or should I eliminate them?
Hello! I am now checking a website that has been migrated months ago from osCommerce to Prestashop.
Intermediate & Advanced SEO | | teconsite
While I was checking crawl errors in search console I found a lot of 404s coming from the last website. The urls are mainly 4 types: popup_image.php?pID=125&osCsid=507c27261ba5ca2568f06ce5bad2ebc9 product-friendly-url-pr-125%3FosCsid.... product-friendly-url-p-125%3FosCsid..... products_new.php?page=228 I've have realized that the parameter pId, and the number that comes after pr- and p- is the product Id in the new website, so I think our team will be able to create an script to redirect those. My question is: Is it ok to send several urls to the same url?. I mean, the popup_image.php was not the product page, as its name says it's more like a popup page. We don't have now a pop up page for images, so I was thinking to send that url to the product page. the one with the pr- was product review page the one with the p- was the product page I was thinking on redirecting the 3 of them to the product page? Should I? Or should I just redirect the last one (p-) and eliminate the others from the index? And... the ones with products_new.php?page=228 I was thinking to redirect all to the page 1 of new products. Is it ok? thank you!0 -
How do you 301 redirect URLs with a hashbang (#!) format? We just lost a ton of pagerank because we thought javascript redirect was the only way! But other sites have been able to do this – examples and details inside
Hi Moz, Here's more info on our problem, and thanks for reading! We’re trying to Create 301 redirects for 44 pages on site.com. We’re having trouble 301 redirecting these pages, possibly because they are AJAX and have hashbangs in the URLs. These are locations pages. The old locations URLs are in the following format: www.site.com/locations/#!new-york and the new URLs that we want to redirect to are in this format: www.site.com/locations/new-york We have not been able to create these redirects using Yoast WordPress SEO plugin v.1.5.3.2. The CMS is WordPress version 3.9.1 The reason we want to 301 redirect these pages is because we have created new pages to replace them, and we want to pass pagerank from the old pages to the new. A 301 redirect is the ideal way to pass pagerank. Examples of pages that are able to 301 redirect hashbang URLs include http://www.sherrilltree.com/Saddles#!Saddles and https://twitter.com/#!RobOusbey.
Intermediate & Advanced SEO | | DA20130 -
301 redirection pointing to noindexed pages
I have rather an unusual situation where a recently launched affiliate site does not have any unique content as its all syndicated content. For that reason we are currently using the noindex,nofollow meta tags to keep the pages out of the search engines index until we create unique content for the pages. The problem is that due to a very tight timeframe with rebranding, we are looking at 301 redirecting (on a page to page basis) another high authority legacy domain to this new site before we have had a chance to add unique content to it and remove the noindex,nofollow tags. I would assume that any link authority normally passed through the 301 would be lost in this scenario but Im uncertain of what the broader impact might be. Has anyone dealt with a similar scenario? I know this scenario is not ideal and I would rather wait until the unique content is up and noindex tags are removed before launching the 301 redirect of the legacy domain but there are a number of competing priorities at play outside of SEO.
Intermediate & Advanced SEO | | LosNomads0 -
Linking Within Website
Hello - I have about 10 landing pages that I am focusing on ranking for and I'm doing okay. My question is should I have all these pages on a drop down menu from my home page or is the innerlinking too much? http://www.kasplacement.com
Intermediate & Advanced SEO | | ksundheim10 -
Duplicate content resulting from js redirect?
I recently created a cname (e.g. m.client-site .com) and added some js (supplied by mobile site vendor to the head which is designed to detect if the user agent is a mobi device or not. This is part of the js: var CurrentUrl = location.href var noredirect = document.location.search; if (noredirect.indexOf("no_redirect=true") < 0){ if ((navigator.userAgent.match(/(iPhone|iPod|BlackBerry|Android.*Mobile|webOS|Window Now... Webmaster Tools is indicating 2 url versions for each page on the site - for example: 1.) /content-page.html 2.) /content-page.html?no_redirect=true and resulting in duplicate page titles and meta descriptions. I am not quite adept enough at either js or htaccess to really grasp what's going on here... so an explanation of why this is occurring and how to deal with it would be appreciated!
Intermediate & Advanced SEO | | SCW0 -
301 redirect for ip address in SERPs
Hi, I've recently had the misfortune of my site's ip address being crawled and indexed by Google, which is causing some duplicate content issues. Due to the nature of the site we're not able to implement a canonical tag to fix this at present. Would a 301 redirect do the trick, and if so, could someone point me to what I'd need to add to our .htaccess file? Many thanks Chris
Intermediate & Advanced SEO | | ChrisHillfd0 -
301 Redirect shenanigans.
So our website (www.FrontlineMobility.com) Has a canonical link redirect to the non www. version. However when I put in website.com it comes up with a small list of links and says this site links to www.website.com. So I'm curious if I used to wrong canonical linking method( that is the method I tried and I placed it in the Head Tags.) I greatly appreciate any assistance in this matter ^.^
Intermediate & Advanced SEO | | FrontlineMobility0 -
Redirecting Powerful Domains
What do you do if you have a client that never implemented a 301 redirect on their domain? For example here are the OSE stats for the URLs; http://url.com PA: 48 DA: 50 LRD: 65 TL: 1,084 FB: 178 FB: 14 T:5 http://www.url.com PA: 51 DA: 50 LRD: 165 TL: 2,271 FB: 178 FB: 14 T:5 G+1:3 My first instincts are to redirect the first one to the second one, but is it too late for that? Will that screw up all of their established stats? Any input or examples of past experiences with this would be great.
Intermediate & Advanced SEO | | MichaelWeisbaum0