htaccess physcially redirect in case of 404

时光总嘲笑我的痴心妄想 提交于 2019-12-25 05:09:27

问题


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.

回答1:


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]




回答2:


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?



来源:https://stackoverflow.com/questions/9380591/htaccess-physcially-redirect-in-case-of-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!