【问题标题】:Redirect one url to another url using .htaccess in codeigniter在 codeigniter 中使用 .htaccess 将一个 url 重定向到另一个 url
【发布时间】:2018-03-29 07:02:05
【问题描述】:

我正在尝试重定向这些 URL:

http://www.example.com/welcome/index

http://www.example.com/welcome

http://www.example.com/index.php

到这个网址:

http://www.example.com/

使用重写规则。

我试过这个(不起作用):

Redirect 301 http://www.example.com/welcome/index$ http://www.example.com/

【问题讨论】:

  • @Alex 我的默认控制器是受欢迎的,它已经将所有链接重定向到我的基本 url 但我想更改 url 也意味着当我打开这个 example.com/welcome/index然后网址更改为 example.com 非常感谢
  • 这是您网站上唯一的控制器吗?
  • 配置文件中的 $config['base_url'] 是什么?
  • @Alex 不,这不是唯一的控制器,而是 acc。要进行 seo,所有链接都必须引用基本 url,即example.com

标签: php .htaccess mod-rewrite url-rewriting codeigniter-3


【解决方案1】:

请在您的路由 .htaccess 文件中添加您的以下代码

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*) http://example.com/$1 [R=301,L] 

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

另外,检查您的$config['base_url'] 并添加您的基本网址:

$config['base_url'] = http://localhost/project/

然后它将像魅力一样工作,您将重定向到另一个没有 index.php 的控制器

Example:  http://localhost/project/home

索引函数的使用:

 www.example.com/welcome/index 

如果需要在 URL 中使用索引功能,请在路由文件中设置路由。

请按上述方式核对

【讨论】:

  • 我已经做到了。此代码用于从 url 中删除 index.php 。但是当有人在 url 部分 www.example.com/welcome/index & www.example.com/welcome 中键入此内容时,如何做到这一点,然后 url 会自动更改为 www.example.com
  • 不完全是当有人键入 www.example.com/welcome/index 时,url clumn 中显示的 url 是 www.example.com
  • 是的,@deep 如果您没有为此提供路线,那么您将始终遇到此问题。
  • 当有人写这个网址 www.example.com/welcome/index & www.example.com/welcome , www.example.com/index.php 时它在路由中不起作用然后它会将 URL 强制为 www.example.com,这是我们项目的基本 URL。请帮助我,我在 10 天后就被困住了
  • 请根据我的代码更新您的代码并设置路由,如 $new_route['welcome/index'] = 'welcome/index'; $new_route['welcome'] = '欢迎/索引';它会工作
猜你喜欢
  • 2013-08-04
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
相关资源
最近更新 更多