【问题标题】:Help with .htaccess RewriteRule. Need to take user from one URL to other帮助 .htaccess RewriteRule。需要将用户从一个 URL 带到另一个
【发布时间】:2009-12-17 17:10:31
【问题描述】:

我想要打字的用户

http://www.example.com/word-of-the-day

被带到

http://www.example.com/index.php?page=word-of-the-day

但我想要

http://www.example.com/word-of-the-day

显示在用户的 URL 中。

我应该在我的 .htaccess 中做什么?我试过了,但是正则表达式 而且 RewriteRule 的语法对我来说太复杂了 弄清楚怎么做。

我们将不胜感激。

编辑:

另外,我怎么能在 htaccess 中这么说 -

如果他们输入http://www.example.com/word-of-the-day,请将他们带到http://www.example.com/index.php?page=word-of-the-day

或者如果他们输入http://www.example.com/something-else,将他们带到http://www.example.com/index.php?page=something-else

否则,只需将他们带到他们输入的 URL。

【问题讨论】:

    标签: .htaccess url-rewriting mod-rewrite


    【解决方案1】:

    下面的条件检查 index.php 没有被请求。如果不适用该规则。这适用于您上面列出的任何场景。

    RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]    
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    

    针对您关于只想对几个特定页面执行此操作的评论,它看起来像这样(作为 Nils 编辑的替代方案):

    RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]
    RewriteCond %{REQUEST_URI}   ^word-of-the-day$ [OR]  
    RewriteCond %{REQUEST_URI}   ^something-else$ [OR]
    RewriteCond %{REQUEST_URI}   ^even-something-else$
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    

    【讨论】:

    • 此 RewriteRule 将适用于任何格式的 URL,对吗?我不要那个。我希望仅针对 2 - 3 个 URL 重写山雀,但其他我需要不理会它们。
    • 如果您只希望它仅用于几页,请查看下面 Nils 的第一个建议,然后将其复制到您想要的每一页。
    【解决方案2】:

    试试这个

    RewriteEngine on 
    RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
    

    或者更灵活

    RewriteEngine on 
    RewriteRule ^(.*)$ index.php?page=$1
    

    未经测试,但它可以工作。

    您的编辑:

    只需手动定义那些特定的 URL:

    RewriteEngine on 
    RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
    RewriteRule ^word-some-example$ index.php?page=some-example
    RewriteRule ^some-other$ index.php?page=some-other
    

    【讨论】:

    • 请查看我的编辑。如果我想像我说的那样指定三个条件怎么办。你的第一段代码会工作吗?
    • 就像,我有 2 - 3 个特定的 URL,我需要为其重写 URL,但对于其余的,我只需要保持原样即可。你得到我的问题了吗?
    • 是的……很常见。您必须手动指定它们。
    • 因此,这将处理这 3 个特定的 URL,并且如果 URL 采用任何其他格式,则不会触及。感谢您的回答!
    • 如果为了使其更具体,我只想在 REQUEST_URI 部分(而不是 QUERY_STRING 部分)中存在字符串“word-of-the-day”时进行重写)?我在我的问题中尝试了类似于RewriteCond %{REQUEST_URI} ^word-of-the-day$ RewriteRule index.php?page=word-of-the-day [L] 的东西,但它不起作用。任何指针?谢谢
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多