【问题标题】:Rewrite URL with .htaccess用 .htaccess 重写 URL
【发布时间】:2010-12-24 17:50:46
【问题描述】:

我有这个网址:http://www.test.com/page.php?k=m1ns

我想要这个:http://www.test.com/r/m1ns

我的 .htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^k/([^/\.]+)/?$ page.php?k=$1 [L]

# force www. in all requests
RewriteCond %{HTTP_HOST} ^test\.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]

# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

但它不起作用。只有非 www -> www 和隐藏 php 规则有效。 如果我把http://www.test.com/page.php?k=m1ns 不重写。

有人知道为什么吗?

谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    
    RewriteRule ^r/([^/]*)$ /page.php?k=$1 [L]
    

    在您的首页.php

    if (strstr($_SERVER['REQUEST_URI'], '/page.php?k=' . $var . '')) {
        header("HTTP/1.1 301 Moved Permanently");
        header("location:http://www.test.com/r/" . $var );
        exit();
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      Options +FollowSymLinks
      RewriteEngine On
      RewriteRule ^r/(.*)/ /page.php?k=$1 [L]
      

      无论是否输入 www,它都应该工作。

      【讨论】:

      • 我已经在我的 ftp 上上传了 .htaccess 并尝试加载“test.com/page.php?k=29ms”,但 url 没有被重写。
      • 转到 test.com/r/29ms,您的脚本会以与 test.com/page.php?k=29ms 相同的方式查看它
      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 2011-08-30
      • 2011-03-19
      • 2022-01-18
      • 2014-04-27
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多