【问题标题】:htaccess redirect if URL contains a certain string如果 URL 包含某个字符串,则 htaccess 重定向
【发布时间】:2011-03-14 07:38:51
【问题描述】:

如果 URL 包含某个单词,我将如何编写 .htaccess 重定向规则?

例如如果它包含foobar,则重定向到index.php

【问题讨论】:

    标签: .htaccess


    【解决方案1】:
    RewriteCond %{REQUEST_URI} foobar
    RewriteRule .* index.php
    

    some variant thereof

    【讨论】:

    • 感谢您的快速回复。工作完美,但只是意识到我需要它来重定向而不是重写。这可能吗?
    【解决方案2】:
    RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
    

    (您的网站内没有空间)

    【讨论】:

    • 如何获取参数(例如 $1)?,RewriteRule ^(.*)foobar(.*)$ /foobar/index.php?q=$1 [L],没有得到任何参数。
    【解决方案3】:

    如果 url 包含一个 certen 字符串,则重定向到 index.php 。您需要匹配 %{REQUEST_URI} 变量以检查 url 是否包含特定字符串。

    如果 uri 在 uri 字符串的任意位置包含 bar,要将 example.com/foo/bar 重定向到 /index.php,您可以用这个:

    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} bar
    RewriteRule ^ /index.php [L,R]
    

    【讨论】:

      【解决方案4】:
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^/foobar/i$ index.php [NE,L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 2014-02-11
        • 1970-01-01
        相关资源
        最近更新 更多