【问题标题】:Redirect all requests to index.php and clean the URL将所有请求重定向到 index.php 并清理 URL
【发布时间】:2014-01-28 20:18:30
【问题描述】:

我想知道是否可以将所有请求重定向到 index.php 不存在的文件/目录,然后清理 url。

所以当我转到www.example.com/test/1 时,我希望它被重定向(重写?)到www.example.com

我尝试了以下操作,它确实正确加载了index.php,但它仍然在网址栏中显示www.example.com/test/1...

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

当然,我仍然希望我的 css 和 js 能够正确加载...

【问题讨论】:

    标签: .htaccess redirect rewrite


    【解决方案1】:

    试试这个。如果我理解,您想以此作为重定向未找到的 404 的一种方式吗?

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [R=301,L]
    

    或者也许这个没有 index.php

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

    【讨论】:

    • 我不希望它成为重定向 404 的一种方式。我只希望所有请求都转到index.php,因为这是我用于这个简单网络应用程序的唯一文件.但由于我使用的是 JS History API,我正在重写 URL 栏,所以当用户刷新它时,我希望它重新加载应用程序并让它们重新启动。
    • 顺便说一下第二个例子!由于项目在子目录中,我不得不重写最后一句!
    猜你喜欢
    • 2016-11-30
    • 2015-04-10
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2012-02-18
    • 2014-07-08
    相关资源
    最近更新 更多