htaccess physcially redirect in case of 404

2019-09-10 02:22发布

To start off I know how to do a simple redirect to a 404 when a page doesn't exist with.

ErrorDocument 404 /index.php

Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.

I've tried this with the [R] redirect flag and obviously it doesn't work.

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]

Update

This is the error I get it rewrites the url to mydomain.com/index.php

I tried to remove the index.php with just / and it didn't redirect but gave the same error below.

The page isn't redirecting properly         

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept
cookies.

2条回答
欢心
2楼-- · 2019-09-10 03:01

Do this:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]

Explanation:

checks whether the file has extension:

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]

If not, checks whether file is present:

RewriteCond %{REQUEST_FILENAME} !-f

If not a file, checks whether it is a folder which is present:

RewriteCond %{REQUEST_FILENAME} !-d

If not append .php. If / is present at the end, remove it and append .php

RewriteRule ([^/]+)/?$ $1.php 

Checks whether the .php appended or whatever extension file is actually a file which is present:

RewriteCond %{REQUEST_FILENAME} !-f

check whether it is a directory:

RewriteCond %{REQUEST_FILENAME} !-d

If not, then it is a invalid file request and redirects it to / RewriteRule ^(.*)$ / [L,R]

查看更多
爷、活的狠高调
3楼-- · 2019-09-10 03:07

This should be your mod_rewrite based 404 handling:

# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]

However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

查看更多
登录 后发表回答