redirecting to homepage rather than 404 error page

2019-04-12 10:25发布

问题:

I'm working with PHP. I have an .htaccess file like below, and it redirects to homepage rather than 404 error page :/ what can be the problem here? Appreciate helps! thanks a lot!

ErrorDocument 404 /new/err404.html
RewriteEngine On
RewriteBase /new/

RewriteRule ^login.html$ index.php?s=login&a=loginDo [QSA,L]
RewriteRule ^logout.html$ index.php?s=login&a=logoutDo [QSA,L]
RewriteRule ^([^/]*).html$ index.php?s=$1 [QSA,L]
RewriteRule ^members/([^/]*)$ index.php?s=profile&username=$1 [QSA,L]
RewriteRule ^([^/]*)/$ index.php?s=listing&search[cityString]=$1 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?s=listing&search[neighborhoodString]=$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*).html$ index.php?s=details&seo_friendly=$3 [QSA,L]

回答1:

The URL path or your error document /new/err404.html without the path prefix /new/ is just err404.html. And that will be matched by your third rule.

You could extend that rule and exclude such the error documents:

RewriteCond $1 !^err[45][0-9][0-9]$
RewriteRule ^([^/]*)\.html$ index.php?s=$1 [QSA,L]


回答2:

I think a more important question, is why you are redirecting 404's to your home page?

The user needs to know that the page is dead/bad, and so does Google. And you need to investigate where the 404's are coming from.

Redirecting your 404's to your home page will tell Google that your links are A-ok, and you may hurt your rankings depending on how many links are being redirected, as Google may see it as duplicate content.

Say you put a rule that affects 5,000 pages - then google comes and sees 5,000 URL's with the same exact content - the home page, how does it know those were supposed to be 404's to begin with?

Fix your 404's. And if you had legitimate content that needs redirecting, well, redirect them to their new page, or the closest semantic page available.



回答3:

Why are you trying to use rewrite rules for your 404 error? Why not just use:

 ErrorDocument 404 /new/err404.html

like you have on the first line? Are you wanting the 404 to stay relative to the directory that the user is trying to go to?



回答4:

try this lines in your .htaccess by changing domain and page name

ErrorDocument 404 http://www.yoursite.com/pagename.html


回答5:

Just through your home page code into the redirect page. Change the code in the redirect page with your home page code. So you will have an /index.html and a /404.html with the same code - your home page. He He