.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
-
301 redirection help needed!
Hi all, So if we used to have a domain (let's say olddomain.com) and we had a new site created at newdomain.com how do we properly setup redirects page to page. Caveat, the urls have changed so for instance the old page oldomain.com/service is now newdomain.com/our-services on the new site. Do we need to have hosting on the old site? Do we need to setup individual 301s for each page corresponding to the new page? Just looking for the easiest way to do this CORRECTLY. Thanks, Ricky
Intermediate & Advanced SEO | | RickyShockley3 -
PushState for redirects
Is it possible to use PushState for redirects from one site to another?
Intermediate & Advanced SEO | | rgamedia_seo0 -
Directory with Duplicate content? what to do?
Moz keeps finding loads of pages with duplicate content on my website. The problem is its a directory page to different locations. E.g if we were a clothes shop we would be listing our locations: www.sitename.com/locations/london www.sitename.com/locations/rome www.sitename.com/locations/germany The content on these pages is all the same, except for an embedded google map that shows the location of the place. The problem is that google thinks all these pages are duplicated content. Should i set a canonical link on every single page saying that www.sitename.com/locations/london is the main page? I don't know if i can use canonical links because the page content isn't identical because of the embedded map. Help would be appreciated. Thanks.
Intermediate & Advanced SEO | | nchlondon0 -
Penguin and 301 redirects...
Hi, I have several questions about starting a new domain due to Penguin. The site is: http://bajajlaw.com. Quick backstory: This site was hit every time Penguin rolled out. No clean-up was done until October 2015. At that time, I took over the project. My efforts include: (1) Remove'em, (2) manual removal, (3) and the Disavow Tool. The HP went from being at around #50 for the target KW (San Diego criminal defense attorney) to about #25. Never really moved higher than that. However, I redid the content for the internal pages (DV, Theft Crimes, etc.) and they are all ranking fairly well (first page or top of 2nd). In short, the penalty only seems to affect the HP, not the internal pages. Instead of waiting for Penguin to roll-out, client wants to move forward with new domain. My questions are as follow: 1. Can I use the same content for the internal pages and 301 from the old internal pages to the new? 2. Should I 301 from the old to the new domain for the HP, or not? 3. If I do a 301 from an internal page to a new internal page, does that have the same effect of doing a 301 from the old HP to the new HP? I have read various opinions on this topic. I'd appreciate feedback from anyone who has experience doing this sort of thing. Thanks. P.s. I'm inclined to wait for P4 to rollout, but given that nobody seems to know when that might be, it's hard for me to advise client to keep waiting for it.
Intermediate & Advanced SEO | | mrodriguez14400 -
Should you cache redirects?
I would like to know what fellow SEO people think, should you cache a redirect? Problems I see with caching redirects are meta refreshes and there might be a slow down in page load, but is it a big issue? Should we cache redirects? Do pages get indexed more if you cache redirects? Our ecommerce product pages are all dynamic, and currently we cache redirects but i'm seeing a lot of meta refresh issues. Another area that cropped up is that, the redirect doesn't pass on query parameters. Our system dumps URLs and they are redirected to SEO ones, but the redirect doesn't pass on parameters like Google Analytic tracking tags. What are your thoughts? Thanks
Intermediate & Advanced SEO | | Bio-RadAbs0 -
Is there anything wrong with this 301 redirect?
I'll keep this one short and sweet 🙂 Many moons ago we used to have several different methods of sorting our products and this change in sort order was achieved by having ?dispmode=list or ?dispmode=grid after the product URL. Best part of a year ago we decided to scrap this feature and 301'd all the ?dispmode URL's back to the base URL. The funny thing is that Google don't seem to have dropped a single one of the old URL's from their index and a search for site:www.refreshcartridges.co.uk dispmode returns almost 8,000 results. This isn't a massive problem but I'd have expected in the past year they'd have picked up on a couple of the 301's and would have started removing the old results. I'd hate to think we were getting any kind of penalisation for duplicate pages. I know the answer to this question is going to be 'just be patient, the old results will disappear' but just to ensure we're not missing anything stupid. I'd really appreciate it if someone could check out www.refreshcartridges.co.uk/brother-c-223.html?dispmode=list to confirm there's nothing more we could be doing to get these old results removed from the index. Many thanks
Intermediate & Advanced SEO | | ChrisHolgate0 -
I need help with htaccess redirect
Hi guys, we have the domain cheats.co.uk, it has always displayed as cheats.co.uk without the www. However it is now showing 2 version of the site, both the www. and the non www. version. I know how to add to the htaccess folder to get the non www. version going to the www. version but i am worried about doing this because the non www. version has always been the one indexed in Google and has a page rank of 3. Should i in fact be redirecting the www.version to the non www. version to keep page rank etc? or will page rank be passed over etc if i redirect to the www. version I hope thats clear Thanks guys Jon
Intermediate & Advanced SEO | | imrubbish0 -
Competitors and Directory Links
Hi guys, wanted to get some input and thoughts here. I'm analyzing many competitor links for a specific client (even other clients actually as well) and come across a pretty heavy directory backlink profiles. has anyone here had success with directory listings? Seem many of the competitors backlinks are coming from directories. What say you?
Intermediate & Advanced SEO | | PaulDylan1