php - 使用 codeigniter 和 htaccess 删除 index.php 和 301 重定向

标签 php apache .htaccess codeigniter mod-rewrite

我有 5 个域都指向同一服务器,出于 SEO 目的,我设置了一个指定主域的规范元链接。

我的问题是,如何从网址中删除 index.php 并将我的所有域 301 重定向到主域?

这是我的.htaccess,用于删除index.php

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

ErrorDocument 404 /index.php

这是我的 .htaccess 到 301 重定向

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_NAME} !^www.example.com$
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]

我尝试将两者合并,但后来得到了一些奇怪的结果,它会重定向域,但将整个网址作为页面的参数...

最佳答案

您需要在路由规则之前进行重定向(将所有内容发送到 /index.php 的规则:

RewriteEngine On

RewriteCond %{SERVER_NAME} !^www.example.com$
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]

RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

ErrorDocument 404 /index.php
DirectoryIndex index.php

关于php - 使用 codeigniter 和 htaccess 删除 index.php 和 301 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777036/

相关文章:

php - 使用 PHP 从非 bool MySQL 导出复选框状态

javascript - 如何导入外部JSON并在php中显示

php - "Call to undefined function mysql_connect()"升级到 php-7 后

php - 如何在 Linux 上使用 Apache 服务器高效地并行运行多个 PHP 脚本

php - paypal 不发送实时 ipn 详细信息

php - 如何删除网址中的问号

php - 关于PHP的内存使用

iphone - 提供 iPhone .ipa 文件的正确 mime 类型是什么?

apache - htaccess 不会重写子域

html - .htaccess 将所有用户重定向到子文件夹(IP 除外)