【问题标题】:How to change/redirect multiple query string URL to simple one?如何将多个查询字符串 URL 更改/重定向为简单的 URL?
【发布时间】:2015-04-10 05:33:35
【问题描述】:

有些网址具有相同的网址结构模式:

//www.example.com/index.php?hop=this&step=that&fgh=1001
//www.example.com/index.php?hop=this&step=that&fgh=1002
//www.example.com/index.php?hop=this&step=that&fgh=1003

//www.example.com/index.php?hop=this&step=that&fgh= 部分相同。
1001~1003 是唯一独特的部分。

我想将它们更改/重定向到:

http://www.example.com/fgh/1001
http://www.example.com/fgh/1002
http://www.example.com/fgh/1003

本网站在 Apache 上运行,带有 mod_rewrite。
我曾尝试处理 .htaccess 文件来重写规则,但目前失败了。

希望有人能帮我重写.htaccess中的代码

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} /index\.php\?hop=this&step=that&fgh=(\d+) [NC]
    RewriteRule ^ /fgh/%1? [R=301,L]
    
    RewriteRule ^fgh/(\d+)/?$ index.php?hop=this&step=that&fgh=$1 [QSA,L,NC]
    

    将这两条规则保留在RewriteEngine 行的下方。

    【讨论】:

    • 我屏蔽了网址,但屏蔽了“example.asia/index.php?hop=this&step=that&fgh=806”。如果您需要真实的网址”,我需要通过短信或电子邮件发送。
    • 我的 htaccess 文件中实际上还有另一个规则,但我不确定哪个会影响什么。我将发布我的 htaccess 文件的一部分。
    • 再次感谢您。当我在 .htaccess 中删除上面的“RewriteRule index\.php - [L]”时,重定向有效,但页面生成 404 错误。我只想更改 URL,但内容。
    • 是的,我现在在浏览器中看到了link,但是页面生成了 404 错误。
    • 是的,它是 joomla。我不确定我是否可以将 .htaccess 更新到最新版本,我使用的版本太旧了。
    【解决方案2】:

    它应该适合你。我试试看。 把它放在.htaccess中

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^hop=(.*)$ [NC,OR]
    RewriteCond %{QUERY_STRING} ^step=(.*)$ [NC,OR]
    RewriteRule (.*) /$1? [R=301,L]
    RewriteRule index/fgh/(.*)/ index.php?fgh=$3
    RewriteRule index/fgh/(.*) index.php?fgh=$3 
    

    【讨论】:

      【解决方案3】:

      让我添加 .htaccess 文件的主要部分;

      RewriteEngine On
      
      RewriteRule index\.php - [L]
      
      
      #This is the code I tried but not working
      RewriteCond %{QUERY_STRING} ^hop=this&step=that&fgh=([^&]+)$ [NC]
      RewriteRule ^(index\.php)?$ /fgh/%1? [R=301,L,NC]
      
      
      ## End of deny access to extension xml files
      RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
      # Block out any script trying to base64_encode crap to send via URL
      RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
      # Block out any script that includes a <script> tag in URL
      RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
      # Block out any script trying to set a PHP GLOBALS variable via URL
      RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
      # Block out any script trying to modify a _REQUEST variable via URL
      RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
      # Send all blocked request to homepage with 403 Forbidden error!
      RewriteRule ^(.*)$ index.php [F,L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !^/index.php
      RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
      RewriteRule (.*) index.php
      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
      

      【讨论】:

        猜你喜欢
        • 2011-10-28
        • 2016-12-31
        • 1970-01-01
        • 1970-01-01
        • 2011-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多