【问题标题】:Redirect Url in codeigniter在 codeigniter 中重定向 URL
【发布时间】:2013-04-30 05:48:24
【问题描述】:

我想在我的 lcalhost 中重定向一个 url,就像用户在 codeigniter 中键入:mystring.localhost/CIlocalhost/CI/Mycontroller/Myaction/mystring 时一样。它现在显示未找到服务器,并说浏览器 Firefox 无法在 www.mystring.localhost 找到服务器。这个怎么做 ?提前致谢。我的.htaccess 中有这个:

    RewriteEngine On     
    RewriteCond %{HTTP_HOST} ^mystring\.localhost\CI\
    RewriteRule ^(.*)$ http://localhost/CI/Mycontroller/myaction/mystring/$1 [R=301]

【问题讨论】:

  • 我的 .htaccess 中有这个:RewriteEngine On RewriteCond %{HTTP_HOST} ^string\.localhost\Tests\ RewriteRule ^(.*)$ localhost/Tests/string/$1 [R=301],但似乎不工作。
  • 不幸的是,这在 localhost 中不起作用,所以我在托管站点中尝试过,但它似乎也不起作用

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


【解决方案1】:

在 Windows 上编辑文件 C:\Windows\System32\drivers\etc\hosts 然后追加:

127.0.0.1 mystring.localhost

所以它将所有请求从mystring.localhost重定向到127.0.0.1

在 Linux 上是相同的语法,但文件位于 /etc/hosts

然后,您必须在 Apache 配置上使用 RewriteCond.htaccess 制作一些 AliasRewriteRule

【讨论】:

  • 现在它重定向到本地主机本身,我希望它去一个特定的控制器和操作。我可以在这里将“mystring”作为变量吗?
  • 您需要在 Apache 上的 .htaccessVirtualHost 进行配置。
  • 我正在寻找 .htaccess ,没关系,我 htink
  • 不幸的是,这在 localhost 中不起作用,所以我在托管站点中尝试过,但它似乎也不起作用
  • 如何在 .htaccess 中使用它?我现在陷入困境
【解决方案2】:

添加到 c:\wamp\bin\apache\ApacheYOUVERSION\conf\extra\httpd-vhosts.conf

NameVirtualHost *.yourdomain.dev
<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/CI"
    ServerName *.yourdomain.dev
</VirtualHost>

c:\wamp\bin\apache\ApacheYOUVERSION\conf\httpd.conf中找到#Include conf/extra/httpd-vhosts.conf,删除开头的#

在您的hosts 文件中:127.0.0.1 yourdomain.dev

将此添加到您的 htaccess:

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yourdomain.dev [NC]  
RewriteRule ([a-z0-9-]+)(/+.*) index.php/$1/%1$2 [QSA]  

【讨论】:

  • 不在这里“mystring”是一个变量,我必须在我的控制器中捕获该字符串才能重用它,这就是我的意思。它不会每次都是“mystring”。
  • 抱歉,Windows 中不可能有本地子域,请看stackoverflow.com/a/9695861/741741
  • 好的,我认为它可以与 .htaccess 一起使用:现在我尝试了这个:RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).localhost/Tests [NC] RewriteRule ([ a-z0-9-]/?)(.*) index.php/$1/%1/$2 [QSA] ,但它不工作
【解决方案3】:

如果我理解正确的话:

-有人会输入 mystring.localhost/CI,您希望它通过操作和值将他们重定向到某个页面?

如果是这样,你可以在\application\config\config.php中设置一个页面作为索引页面

第 30 行:

$config['index_page'] = 'Mycontroller/Myaction/mystring';

否则,在处理您的索引的控制器中,只需在其 __construct 函数中进行重定向。

很可能有一个涉及 HTACCESS 文件的解决方案,但我对此不是很了解。

希望这会有所帮助!

-桂

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2014-04-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    相关资源
    最近更新 更多