Is there a way to redirect the users to the homepage whenever the user enters a wrong url?
Let's say app.php/Contact triggers a 404 error and I would like all 404 errors to be redirected to the homepage?
I am pretty sure I read that somewhere at a time, but can't find it anymore. I think it was a else statement in the config file?
You should extend ExceptionController and redefine showAction() with the logic you need.
Simply as:
if (404 === $exception->getStatusCode()) {
//do your logic
}
Use an .htaccess file and add the following line:
ErrorDocument 404 /home.php
Related
Okay, here's the deal:
I have an aspx page that looks like mysite.com/UnitDetails.aspx?UnitID=123
Right now I've setup my routing to be able to take in something like mysite.com/My/Path/123 and redirect it to UnitDetails.aspx. This works great.
However, what I'd like to be able to also do is redirect the user to the clean url if they type in the aspx page. For example, if you have mysite.com/UnitDetails.aspx?UnitID=123 bookmarked, I'd like it to show up as mysite.com/My/Path/123.
How can I accomplish this? Here's what I have right now in my routing:
RouteTable.Routes.MapPageRoute("unit_details", "{area}/{property}/{unit_id}", "~/UnitDetails.aspx")
Okay, I figured it out!
Just needed to add this into my UnitDetails.aspx code behind in the Page_Load:
//issue a 301 if legacy route requested to boost SEO
if (Request.Url.AbsolutePath.Contains("UnitDetails.aspx"))
{
var unit = BeachGuide.Models.Unit.GetById(Convert.ToInt32(Request.Params["UnitID"]));
Response.RedirectPermanent(unit.relative_url);
}
So now if they request the new url, they won't get caught in an infinite loop. If they request the old url, we have a base case that redirects them to the new url via a 301 redirect. This will help search engines know to use the new route as the canonical url.
I have a custom folder called /portal/ that takes arguments like /portal/?id=123 or /portal/?id=321 etc.
In the /portal/index.php file I included the following which automatically redirects users to the WP login if the user is not authenticated.
include_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
If not logged in they are automatically redirected to http://www.domain.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.domain.com%2F&reauth=1.
How do I fix the redirect_to argument so it includes the path and arguments? I want the redirect to look like this:
http://www.domain.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.domain.com%2Fportal%2F?id%3D123&reauth=1
This way, once the user logs in, they will be redirected to the page they were trying to access in the first place.
First, including wp-load.php isn't recommended - Don't include wp-load, please - for several reasons.
Presumably you have Wordpress configured to require a login, which is why are you are getting a redirect to the login page, and because it is happening directly from your include, you can't insert any filters to change the redirect before it's loaded, and using them afterwards is moot because the redirect will have already fired.
If this was constructed as a plugin it would be loaded by wp-load, and you would have access to the login_url filter, which you can use to add a redirect as in this example: http://trepmal.com/filter_hook/login_url/.
I was able to come up with a hack that checks for the existence of a cookie key begining with wordpress_logged_in_. If the cookie key doesn't exist then I redirect to the login page to http://www.domai.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.domain.com'.urlencode($_SERVER['REQUEST_URI']).
I use ASP.Net URL Routing for SEO and when I run into a routename that is bad in (i.e. /Games/{RouteName}) I want to pass it to my 404 page for handeling. I check the DB to make sure {RouteName} is a valid name for a game. If the RouteName is valid I want to continue processing as normal but if the DB comes back and does not find a corisponding RouteName I would like to some how have the server act as if a truly bad URL was entered. How can I best accomplish this?
I would suggest throwing a 404 error inside your logic that determines it is not valid, e.g.
if (!<validRouteName>) {
return new HttpStatusCodeResult(404, "Game not found.");
}
Doing a bit of messing around in IIS I have figure this out. As endyourif suggested I need to throw a 404 resposne in my code behind, which was done by Response.StatusCode = 404; I also had to configure IIS's Error Pages for the 404 to point to my 404.aspx page.
I am trying to figure out how to make it so that if a user types in a url and gets a 404 error message, they get routed to wordpress to see if it can figure out the url.
Currently I have a site
xyz.com where it is a wordpress site. I am designing a replacement where xyz.com will be a codeigniter site. I will keep the wordpress part xyz.com/blog will be wordpress.
I would like the old urls that are on search engines to be able to redirect to the blog that still has the same data as before, but is now under /blog.
What would be the best way to accomplish this.
Thanks
This would send any 404s to a custom "/error" controller, along with the original requested uri string:
class MY_Exceptions extends CI_Exceptions{
function __constructor(){
parent::CI_Exceptions();
}
function show_404($page=''){
header('Location: /error'.$_SERVER['REQUEST_URI']);
exit;
}
}
the controller could then get the URI string, add "/blog/" in front of it, and check if the file exists. If it does, redirect... if not, go to another error page. So in the controller "error":
function index()
{
$this->load->helper('url');
// remove "/error" from the uri
$uri = substr($this->uri->uri_string(),0,6);
$newuri = base_url() . "blog" . $uri;
if(file_exists($newuri)):
redirect($newuri,'location',301);
else:
redirect("/anotherErrorPage");
endif;
}
I hope that helps!
update: originally thought to use htaccess, but since everything in CI goes through, index.php.... that really isn't needed.
In my site, I have used IIS7's URL rewrite module to redirect URLs like http://mysite.com/File.aspx?Name=SomeName into http://mysite.com/SomeName.
It appears that IIS7 has created a corresponding rule check, so that any URL of the sort http://mysite.com/SomeURL is redirected to File.aspx. This is fine in most cases, when the URL is correctly rewritten.
The problem is that in some cases, the file no longer exists - http://mysite.com/SomeName2 will still get redirected to http://mysite.com/File.aspx?Name=SomeName2.
I want to show a custom 404 error page for this URL - how do I trigger the 404 error in global.asax (I have set application error logging and handling in global.asax)? The below code doesn't work.
Response.Status = "404 Not Found"
Response.AddHeader("Location", "http://mysite.com/Invalid-File.aspx?" & Request.QueryString.ToString)
It just shows the ugly default IIS 404 error page. Adding a customerror in web.config doesn't help.
Thanks for your help!
When setting a status code you need to prevent IIS taking over based on your new error code, make sure to set Response.TrySkipIisCustomErrors like this:
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
Response.AddHeader("Location", "http://mysite.com/Invalid-File.aspx?" & Request.QueryString.ToString)
Did you try setting the 404 error page in the IIS admin equal to what you're specifying in the web.config? I wonder if .net isn't jumping into the pipeline at the right time.
you could potentially use the Application_Error method and then do a response.redirect to the required page with the correct status code