.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
-
Using a Reverse Proxy and 301 redirect to appear Sub Domain as Sub Directory - what are the SEO Risks?
We’re in process to move WordPress blog URLs from subdomains to sub-directory. We aren’t moving blog physically, but using reverse proxy and 301 redirection to do this. Blog subdomain URL is https://blog.example.com/ and destination sub-directory URL is https://www.example.com/blog/ Our main website is e-commerce marketplace which is YMYL site. This is on Windows server. Due to technical reasons, we can’t physically move our WordPress blog to the main website. Following is our Technical Setup Setup a reverse proxy at https://www.example.com/blog/ pointing to https://blog.example.com/ Use a 301 redirection from https://blog.example.com/ to https://www.example.com/blog/ with an exception if a traffic is coming from main WWW domain then it won’t redirect. Thus, we can eliminate infinite loop. Change all absolute URLs to relative URLs on blog Change the sitemap URL from https://blog.example.com/sitemap.xml to https://www.example.com/blog/sitemap.xml and update all URLs mentioned within the sitemap. SEO Risk Evaluation We have individual GA Tracking ID and individual Google Search Console Properties for main website and blog. We will not merge them. Keep them separate as they are. Keeping this in mind, I am evaluating SEO Risks factors Right now when we receive traffic from main website to blog (or vice versa) then it is considered as referral traffic and new cookies are set for Google Analytics. What’s going to happen when its on the same domain? Which type of settings change should I do in Blog’s Google Search Console? (A). Do I need to request “Change of Address” in the Blog’s search console property? (B). Should I re-submit the sitemap? Do I need to re-submit the blog sitemap from the https://www.example.com/ Google Search Console Property? Main website is e-commerce marketplace which is YMYL website, and blog is all about content. So does that impact SEO? Will this dilute SEO link juice or impact on the main website ranking because following are the key SEO Metrices. (A). Main website’s Avg Session Duration is about 10 minutes and bounce rate is around 30% (B). Blog’s Avg Session Duration is 33 seconds and bounce rate is over 92%
Intermediate & Advanced SEO | | joshibhargav_200 -
Getting SEO Juice back after Redirect
Hi, On my website, many product pages were redirected over time to its product category, due to the product being unavailable. I understand with a 301 redirect, the final URL would have lost about 15% of the link juice. However - if after some time (e.g. 2 months, or 1 year) I remove the redirection - is the original page going to have any SEO juice, or did it already lose all of it? Thanks,
Intermediate & Advanced SEO | | viatrading10 -
Setting up 301 Redirects after acquisition?
Hello! The company that I work for has recently acquired two other companies. I was wondering what the best strategy would be as it relates to redirects / authority. Please help! Thanks
Intermediate & Advanced SEO | | Colin.Accela0 -
Directory Quality for Citation Building
Hello All, Just started to work on a new clients site that has been hit with multiple google penalties. I was looking at their backlink profile and noticed they have numerous links from what seem to be very low quality directory websites. My question is, when building citations and looking for directories to submit to, what makes one directory more credible then another one? If most of them are just publishing links and business information, why does google consider one credible and the other spammy? Clearly with some it's easy to tell if they are credible or not, but with some it is not as easy. Should you only really be submitting to the best of the best or are some lower lever ones ok too? Have read a few things on this topic, but most is older and just want to hear what people have to say on this today. Thanks.
Intermediate & Advanced SEO | | Whebb0 -
Warning about a 302 redirect
Hello everyone, I'm testing the pro software and recently I installed an SSL Certificate on one of the websites I'm monitoring, I put in place an .htaccess directive to force all traffic to the secure version of the site (https) and I noticed how this raised a warning because my directive is forcing the traffic with a 302 redirect. These are the lines: _RewriteCond %{SERVER_PORT} 80 _ RewriteRule ^(.*)$ https://example.com/$1 [R,L] I understand that this is not good so I figured since I'm already redirecting all www to -www I can force traffic that arrives trying to use www to the secure version like so: RewriteCond %{HTTP_HOST} !^example.com$ RewriteRule (.*) https://example.com/$1 [R=301,L] But this is not 100% effective because if someone visits the site directly on the -www version this person wont get redirected hence it wont be forced to see the https. So my question is: does anybody know of an alternate way to force traffic to the secure socket using a 301 instead of a 302? Oh boy, just by writing the question I think I may have figured it out, I'll post it anyways because (1) I could be wrong and (2) It could help someone else. It just hit me but the directive that is forcing www to -www specifies what type of redirect to do here [R=301,L]. So to try to answer my own question before even posting it this could probably do the trick: _RewriteCond %{SERVER_PORT} 80 _ _RewriteRule ^(.*)$ https://example.com/$1 [_R=301,R,L] I'll be testing it out ASAP and again I'll post the question anyways just in case it doesn't work, in case someone has a good suggestion or to help someone that could be in the same situation. If this is turns out right I will need someone to slap me in the face 😐
Intermediate & Advanced SEO | | stevenpicado0 -
How to fix a canonical problem with no htaccess?
Basically I need to find a way to fix my canonical problem. The www and no-www version of my site each show up. This creates duplicate content. I have no htaccess with yahoo. How else can I fix this problem? Does anyone have a sample code I could use?
Intermediate & Advanced SEO | | bronxpad0 -
How to Hide Directories in Search?
I noticed bad 404 error links in Google Webmaster Tools and they were pointing to directories that do not have an actual page, but hold information. Ex: there are links pointing to our PDF folder which holds all of our pdf documents. If i type in , example.com/pdf/ it brings up a unformated webpage that displays all of our PDF links. How do I prevent this from happening. Right now I am blocking these in my robots.txt file, but if i type them in, they still appear. Or should I not worry about this?
Intermediate & Advanced SEO | | hfranz0 -
How to 301 redirect ASP.net URLS
I have a situation where a site that was ASP.net has been replaced with a WordPress site. I've performed a Open Site Explorer analysis and found that most of the old pages, ie www.i3bus.com/ProductCategorySummary.aspx?ProductCategoryId=63 are returning a HTTP Status = NO DATA ... when followed ends up at the 404 catch-all page. Can I code the standard 301 Redirects in the .htaccess file for these ASP URLs? If not, I'm open to suggestions.... Thanks Bill
Intermediate & Advanced SEO | | Marvo0