重写没有302重定向的URL

时间:2015-05-16 12:19:07

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

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

  • 第一个域(domain1.com)关注所有网站
  • 第二个域(domain2.com)只关注一个函数来执行 url shortener

这是我的.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我会重定向到http://domain2.com/url/57b97f2并进行302重定向。

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

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

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

我的路线是:

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

Webadmin控制器:

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

1 个答案:

答案 0 :(得分: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]

请注意,我已经简化了很多代码。

另外,我没有对此进行测试,但它应该有效(理论上)。