使用htaccess删除index.php

时间:2018-05-22 10:20:02

标签: php .htaccess

我想使用htaccess从url中删除index.php。 代码示例:

Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

例如:如果我的网址https://url.com/index.php然后如何制作https://url.com?。

第二个问题是,如果某人输入了包含非索引的https://url.com/directory,那么如何在没有index.php的情况下将其重定向到主域名?

我在root用户使用简单的单索引文件而不是框架。并使用cloudflare dns。

当我用RewriteRule ^ %1 [R=301,L]替换最后一行然后工作。

例如:https://url.com/demohttps://url.com/demo/demo1(不存在目录),然后在没有https://url.com的情况下重定向到index.php

但当网址为https://url.com/index.php时,它仍会显示相同的网址https://url.com/index.php。如何删除index.php

5 个答案:

答案 0 :(得分:1)

在您的项目根目录中,请创建或放置.htaccess文件。并添加以下内容。

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

答案 1 :(得分:1)

您可以在站点根目录中使用这些规则.htaccess:

Options +FollowSymlinks -Indexes
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

答案 2 :(得分:0)

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

答案 3 :(得分:0)

RewriteEngine on
RewriteBase /Projectname
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

答案 4 :(得分:0)

如果使用CodeIgniter

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /ProjectName/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

否则

    <IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

这是从URL隐藏index.php的基本规则。把它放在你的root .htaccess文件中。

必须使用PHP启用mod_rewrite,这适用于高于5.2.6的PHP版本。

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

    RewriteEngine On     RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]