【问题标题】:trying to rewrite url via htaccess only in specific case仅在特定情况下尝试通过 htaccess 重写 url
【发布时间】:2017-04-12 05:21:13
【问题描述】:

对于域 www.example.com,我想将用户定向到 index.php。使用

DirectoryIndex index.php

我这样做。但是,如果它们包含斜杠和非文件类型的值,我想将它们重定向到不同的页面并将该值用作 php get 变量:

www.example.com/hello lands on a_different_page.php?var=hello

但我不想打扰以“php”结尾的请求,例如:

www.example.com/page.php

不应尝试重定向。而且对纯 url 的请求也不应该改变:

www.example.com goes to index.php

我已经找到了一些方法来实现其中的一些,但它们总是会干扰其他请求,这是一个意想不到的结果。任何帮助将不胜感激 - 我对 htaccess 重写的研究没有帮助。

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您可以使用以下 htaccess :

    DirectoryIndex index.php
    RewriteEngine on
    #rewrite /foobar unless a real file and dir to /page.php?var=foobar
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.+) /page.php?var=$1 [L]
    

    【讨论】:

      【解决方案2】:

      处理这个问题的最好方法是不要重定向任何指向已经存在的东西。

      RewriteCond %{REQUEST_FILENAME} -f
      RewriteRule (.*) a_different_page.php?var=$1
      

      【讨论】:

        【解决方案3】:

        希望它能正常工作。对我来说工作得很好。

        RewriteEngine On
        
        Options -Multiviews
        #For rewriting / request to index.php
        RewriteCond %{REQUEST_URI} ^/?$
        RewriteRule ^(.*)$ /index.php [L]
        
        #For redirecting /some/anonymous/request to some_different_page.php?var=/some/anonymous/request when request uri does not ends with .php
        RewriteCond %{REQUEST_URI} !\.(php)$
        RewriteRule ^(.*)$ /some_different_page.php?var=$1 [R=301,L,QSA]
        

        【讨论】:

          猜你喜欢
          • 2011-04-26
          • 1970-01-01
          • 1970-01-01
          • 2014-07-20
          • 1970-01-01
          • 2019-02-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多