htaccess的physcially重定向的情况下404重定向、情况下、htaccess、physcially

2023-09-02 01:09:28 作者:言清欢

要开始了,我知道该怎么做一个简单的重定向到 404 时,页面不存在与

 的ErrorDocument 404的index.php
 

就是我想要做的是一个物理重定向到索引页面。因此,如果在 mydomain.com/abc 用户类型和页面不存在它的实际重定向到索引页面。我不希望URL保持不变与错误的地址。是的,我知道它不是什么大不了的事,但我的错误。我可以通过具有自定义404页面重定向到主页做到这一点,但我不希望这样。

我已经试过这与 [R] 重定向标志,显然这是行不通的。

 选项-MultiViews
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则^(。*)$ $ 1.PHP [L]
重写规则^(。*)$的index.php [L,R]
 
301重定向怎么做

更新

这是我得到它重写URL以 mydomain.com/index.php

错误

我试图删除的index.php 只有 / 并没有重定向,但作出了同样的错误如下:

 该网页重定向不正确

Firefox已经检测到服务器重定向该地址的请求的方式,将永远不会完成。

这个问题可以有时通过禁用或拒绝接受引起
曲奇饼。
 

解决方案

做到这一点:

  RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{REQUEST_URI}!(?:  W +)$ [NC]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则([^ /] +)/ $ $ 1.PHP

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ / [L,R]
 

说明:

检查文件是否有扩展名:

 的RewriteCond%{REQUEST_URI}!(?:  W +)$ [NC]
 

如果没有,检查文件是否present:

 的RewriteCond%{} REQUEST_FILENAME!-f
 

如果不是文件,检查它是否是一个文件夹是present:

 的RewriteCond%{} REQUEST_FILENAME!-d
 

如果不追加的.php 。如果 / 是present末,将其删除,并追加的.php

 重写规则([^ /] +)/ $ $ 1.PHP
 

检查是否的.php 追加或任何扩展名的文件实际上是一个文件,该文件是present:

 的RewriteCond%{} REQUEST_FILENAME!-f
 

检查它是否是一个目录:

 的RewriteCond%{} REQUEST_FILENAME!-d
 

如果没有,那么它是一个无效的文件请求,并将其重定向到 /     重写规则^(。*)$ / [L,R]

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.

解决方案

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]