PHP Codeigniter Htaccess.删除控制器名称和index.php并重写url [英] PHP Codeigniter Htaccess. Remove controller name and index.php and rewrite url

查看:23
本文介绍了PHP Codeigniter Htaccess.删除控制器名称和index.php并重写url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在Codeigniter中从事一个新项目,除一件事是url之外,其他一切工作都很好

Hello all i have been working on a new project in Codeigniter and everything is working great except one thing that is url

我想要从我的URL中删除控制器名称 main 和单词 index.php 就像我的网址现在是这样

What i want is to remove the controller name main and the word index.php from my URL like my url is right now like this

当前网址:www.domain.com/index.php/main/contact

Current URL: www.domain.com/index.php/main/contact

其中main =控制器名称,联系人是功能名称

所需的网址:www.domain.com/contact

Needed url: www.domain.com/contact

我知道我可以通过htaccess删除index.php和控制器名称,并且还实现了bt 的功能,问题是我网站上的所有链接都已被列为domain.com/index.php/main/contact ,所以我想要一些可以自动将URL重定向到所需的URL,而无需使用index.php和控制器名称的工具.

i know that i can remove index.php and controller name by htaccess and i have also achieved it some what bt the problem is all of my links in the website is already lined as domain.com/index.php/main/contact so i want something which can automatically redirect the url to the required url without index.php and controller name.

注意:控制器名称始终为"main"

Note: controller name is always "main"

我的htaccess文件在根文件夹中是这样的:

My htaccess file is like this in root folder:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Header set Access-Control-Allow-Origin "*"

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

以这种方式提供的任何帮助均受到高度赞赏. 我也不希望我的表单提交有任何问题,因为它们具有操作网址等.

Any help in this manner is highly appreciated. also i do not want to have any problem with my form submitions as they have the action urls etc.

推荐答案

将此规则插入RewriteEngine On行以下作为您的最高规则:

Insert this rule just below RewriteEngine On line as your topmost rule:

# remove /index.php/main/ from URLs
RewriteCond %{THE_REQUEST} \s/+index\.php/main/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^index\.php/main(/.*)?$ https://%1$1 [L,NC,NE,R=301]

这篇关于PHP Codeigniter Htaccess.删除控制器名称和index.php并重写url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆