使用htaccess删除index.php

问题描述 投票:0回答:6

我想使用 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

我在根目录使用简单的单个索引文件而不是框架。并使用 cloudflare dns。

当我用

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

例如:

https://url.com/demo
https://url.com/demo/demo1
(不存在目录)然后它会重定向到
https://url.com
而不带
index.php

但是当 url 为

https://url.com/index.php
时,它仍然显示相同的 url
https://url.com/index.php
。如何删除
index.php

php .htaccess
6个回答
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]

1
投票

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

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

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

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]

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 的基本规则。将其放入根 .htaccess 文件中。

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

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

重写引擎开启 重写基址 /

# 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]


0
投票

我在 ec2-apache 服务器上使用 php-laravel 应用程序。这是唯一对我有用的解决方案。在 apache2.conf 上更改此设置:

<Directory "/var/www/<your-app>"> 
 AllowOverride All
</Directory>

无需 .htaccess。

© www.soinside.com 2019 - 2024. All rights reserved.