【问题标题】:.htaccess redirecting errors.htaccess 重定向错误
【发布时间】:2010-11-18 09:23:11
【问题描述】:

首先我使用的是 Codeigniter 框架,所以这个问题是 CI 处理 URL 以及我使用 mod_rewrite 设置的当前重定向的一种解决方法。

我正在尝试获取像 /?gclid=somestringgoeshere 这样的 URL 以重定向到 /index.php?/home/gclid/somestringgoeshere。

我设置的当前.htaccess在下面

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
    RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
    RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
    RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
    RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
    RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # This last condition enables access to the images and css folders, and the robots.txt file
    # Submitted by Michael Radlmaier (mradlmaier)

    RewriteCond $1 !^(index\.php|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

我正在尝试在第一次设置重写条件和规则之前使用以下代码来捕获它,然后再尝试其他任何东西

RewriteRule ^?gclid=(.*)$ index.php?/home/gclid/$1 [L]

和

RewriteRule ^\?gclid=(.*)$ index.php?/home/gclid/$1 [L]

和

RewriteRule ^/?gclid=(.*)$ index.php?/home/gclid/$1 [L]

要么不显示正确的页面,要么出现 500 内部错误。

【问题讨论】:

    标签: .htaccess mod-rewrite codeigniter redirect


    【解决方案1】:

    URI 的查询只能用RewriteCond directive 进行测试:

    RewriteCond %{QUERY_STRING} ^gclid=(.*)
    RewriteRule ^$ index.php?/home/gclid/%1 [L]
    

    或更一般的(将考虑进一步的查询参数):

    RewriteCond %{QUERY_STRING} ^([^&]*&)*gclid=([^&]*)
    RewriteRule ^$ index.php?/home/gclid/%2 [L]
    

    哦,顺便说一句:RewriteCond 指令只对应于后面的第一个 RewriteRule 指令。

    【讨论】:

    • 它需要 ?在这样的网址中 ^?gclid=(.*) 但这也显示错误
    • 您只能使用RewriteRule 指令测试URI 路径。 URI 查询(从第一个 ? 到之后的第一个 # 的部分)只能使用 %{QUERY_STRING} 变量访问。这只能通过RewriteCond 指令实现。所以我的例子应该有效。
    • 发现我从您的示例中复制的文本存在一些问题,它插入了一个奇怪的字符。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 2012-03-04
    • 2020-02-15
    • 1970-01-01
    • 2011-08-31
    • 2011-07-07
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多