.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
-
Redirecting Pages During Site Migration
Hi everyone, We are changing a website's domain name. The site architecture will stay the same, but we are renaming some pages. How do we treat redirects? I read this on Search Engine Land: The ideal way to set up your redirects is with a regex expression in the .htaccess file of your old site. The regex expression should simply swap out your domain name, or swap out HTTP for HTTPS if you are doing an SSL migration. For any pages where this isn’t possible, you will need to set up an individual redirect. Make sure this doesn’t create any conflicts with your regex and that it doesn’t produce any redirect chains. Does the above mean we are able to set up a domain redirect on the regex for pages that we are not renaming and then have individual 1:1 redirects for renamed pages in the same .htaccess file? So have both? This will not conflict with the regex rule?
Intermediate & Advanced SEO | | nhhernandez0 -
Browser Cacheing - HTTPS redirects to HTTP
Howdy lovely Moz people. A webmaster redirected https protocol links to http a number of years ago in order to try and capture as many links as possible on a site we now manage. We have recently tried to implement https and realised that because of this existing redirect rule, they are now causing infinite loops when trying to test an http redirect. http redirecting to https redirecting back to http, etc. The https version works by itself weirdly enough. We believe that this is due to the permanent browser caching. So unless users clear their cache, they will get this infinite loop. Does anyone have any advice on how we can get round this? a) index both sites and specify in GSC that the https is the canonical version of the site and hope that Google sees that and removes the http version for the https version b) stick with http as infinite loops will kill the site c) ??????????? Thanks all.
Intermediate & Advanced SEO | | HenryFrance0 -
How to handle individual page redirects on Wix?
I switched from one domain to another because I wanted a domain that had our company name so it was more brand-y. However, the old domain had better DA/PA. Originally I set up a global 301 from the old to the new, but now I'm finding that I actually need to set up individual 301's from each URL of the old site, or at least from each page. However, I am using Wix so it looks like I can't always do URL-URL 301's, although I can redirect any URL to a page on the new website. The problem is that, in some cases, the content on the new site is different (or, for example, I can only link a particular blog post on the old site back to the new site's blog's main page). How closely do URLS/pages need to resemble each other for link juice to be transferred? Also, should I try to set up all these redirects manually or bite the bullet and go back to using the old domain? The problem is that I did a lot of beginner SEO junk for the new domain, like submitting to a few higher-quality directories, and getting our website on various industry resource sites, etc. I'd need to re-do this entirely if I go back to the old page. What do you think?
Intermediate & Advanced SEO | | BohmKalish1230 -
SEO for a redirected domain name
Our client is a law firm with a name that is challenging to spell. We have procured a domain name for them that is catchy, easy to spell, and plays well into their brand, or at least the current campaign. We're using the campaign domain to direct traffic to their website with a 301 redirect. We have placed the campaign domain in a variety of offline mediums including print and outdoor. The client is currently in the number 1 spot for a good number of our highest priority keywords, so I do not want to do anything to jeopardize that. I'm also not sure this campaign will be their "brand" long-term so I don't want to risk making a switch and making it back. So for now, I'm most comfortable leaving the campaign domain as a redirect to their primary domain. Recently, the client approached me complaining (legitimately) that when people google the campaign domain, they are brought to search results for an entirely different domain because Google "corrects" the domain name for them. This is obviously a bad thing, with many users defaulting to entering urls into Google instead of the address bar. If you tell Google that it was wrong about the autocorrection, our site is in the number 1 position. I liken the situation to Overstock.com using O.co as their offline domain, but overstock.com as their online domain. But imagine if you googled o.co and google brought you to a list of results for "on.co" because it assumed you fat-fingered it. Is there anything I can do to prevent the domain name from getting corrected by Google?
Intermediate & Advanced SEO | | steverobinson0 -
.htaccess 301 Redirect Help! Specific Redirects and Blanket Rule
Hi there, I have the following domains: OLD DOMAIN: domain1.co.uk NEW DOMAIN: domain2.co.uk I need to create a .htaccess file that 301 redirects specific, individual pages on domain1.co.uk to domain2.co.uk I've searched for hours to try and find a solution, but I can't find anything that will do what I need. The pages on domain1.co.uk are all kinds of filenames and extensions, but they will be redirected to a Wordpress website that has a clean folder structure. Some example URL's to be redirected from the old website: http://www.domain1.co.uk/charitypage.php?charity=357 http://www.domain1.co.uk/adopt.php http://www.domain1.co.uk/register/?type=2 These will need to be redirected to the following URL types on the new domain: http://www.domain2.co.uk/charities/ http://www.domain2.co.uk/adopt/ http://www.domain2.co.uk/register/ I would also like a blanket/catch-all redirect from anything else on www.domain1.co.uk to the homepage of www.domain2.co.uk if there isn't a specific individual redirect in place. I'm literally tearing my hair out with this, so any help would be greatly appreciated! Thanks
Intermediate & Advanced SEO | | Townpages0 -
Domain Name Redirect Question
My agency just built a new website for a client who is a franchisee. It's not launched yet - it's currently under an IP address. I suggested to client that he buy a keyword-rich domain name for it, which he did. Then he found out that the franchisor will not allow it to be his main domain name. They want him to use a domain name with the franchisor name in it. But they WILL allow him to put a 301 redirect on that franchisor-approved domain name, and redirect it to his keyword-rich domain name. He is interested in having my agency perform an SEO Campaign for this new website. But would SEO and link marketing work for a website that has a new non-keyword domain name that 301 redirects to a new keyword-rich domain name?
Intermediate & Advanced SEO | | netsites0 -
301 Redirect using rewrite rule in .htaccess
Hi guys, I have these types of URLs with the format below that are seen as duplicate contents http://www.mysite.com/index.php?a=11&b=15&d=3&c=1 I wanted to permanently redirect them to my homepage. I am thinking if this is possible in .htaccess using rewrite conditions? Thanks in advance...
Intermediate & Advanced SEO | | Trigun0 -
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