【问题标题】:Rewrite an URL without 302 redirect重写没有 302 重定向的 URL
【发布时间】:2015-07-28 07:52:27
【问题描述】:

我想知道,如何在没有 302 重定向的情况下重写 URL。
该网站被 2 个域定位。

  • 第一个域 (domain1.com) 聚焦所有网站
  • 第二个域(domain2.com) 只关注一个功能来执行url 缩短器

这是我的.htaccess

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain2.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2.com$
RewriteRule ^([A-Za-z0-9-]+)$ http://domain2.com/url/$1 [L]

Rewritecond %{HTTP_HOST} domain2.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://domain2.com/soon/ [QSA,L,R=301]

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

当我打电话给我的猎枪http://domain2.com/57b97f2 时,我会通过 302 重定向重定向到 http://domain2.com/url/57b97f2

如何避免这种不需​​要的 302 重定向?

======= 编辑 ======= :

url() 函数的控制器名为:webadmin

我的路线是:

$route['default_controller'] = 'webadmin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(.+)'] = 'webadmin/$1';

网络管理员控制器:

function url($code)
{
    //do something
}

【问题讨论】:

  • 你必须从你的规则中删除http://domain2.com(否则你会得到一个重定向,即使有L标志)。但是domain1domain2 必须共享同一个文档根文件夹才能使其以这种方式工作
  • Domain1 和 Domain2 实际上关注同一个文档根目录。
  • 好的,第一个好点。现在,你想要什么而不是 302 重定向?另外,url/57b97f2(例如)是一个真实的文件/文件夹吗?
  • 当我调用我的缩短 url 时,我希望调用我的函数 url() 而不重定向到 domain2.com/url/57b97f2 而是重定向到 domain2.com/57b97f2
  • 那么只需从您的规则中删除 http://domain2.comRewriteRule ^([A-Za-z0-9-]+)$ /url/$1 [L] 。这意味着如果您现在转到http://domain2.com/something,它将在内部重写为/url/something(然后您必须在那里处理它)

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


【解决方案1】:

请尝试以下方法:

Options +FollowSymlinks

RewriteEngine on

# 1. If we're on the root of domain2.com, temporarilty redirect to
#    the `/soon` handler
#    Note: This redirect should really be temporary, as it is a
#          landing page for your soon-to-be-released app/site.

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$
RewriteRule ^$ /soon [R=302,L]

# 2. If we're on domain2.com, rewrite short URIs to the `/url` handler
#    Note the use of the `N` flag which causes the ruleset to start
#    again from the beginning using the result of the rewrite. This
#    will cause the rewritten URI to be passed to `index.php` (the
#    last RewriteRule).
#    Also added is the NC flag, which may or may not be better than
#    specifying `A-Z` in the pattern's expression.

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$
RewriteRule ^([a-z0-9-]+)$ /url/$1 [NC,N]

# 3. Redirect application/system directory requests to index.php

RewriteRule ^(application|system) /index.php?/$1 [L]

# 4. For everything else (sans files and directories), rewrite to index.php

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

请注意,我已经简化了您的大部分代码。

另外,我还没有测试过这个,但它应该可以工作(理论上)。

【讨论】:

  • 感谢简化代码,但我仍然拥有404 Page Not Found。当我去domain2.com/codeurl
  • 请查看我的更新 - 我已将第二个块更改为显式重写为 index.php?/url/$1
  • 我已经用更多信息编辑了我的回答,我仍然有你更新的 404 错误:/
  • 随着您的更新,我需要添加 /url/code 以使其正常工作,否则我有 404
  • 我很抱歉,但我的更新确实有缺陷(回滚了我的答案)。也就是说,最好在 CI 本身内部处理这个问题,而不是在.htaccess
猜你喜欢
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2017-07-05
  • 2014-09-16
  • 1970-01-01
  • 2011-05-16
  • 2017-05-29
相关资源
最近更新 更多