用的.htaccess重写URL [英] Rewrite URL with .htaccess

查看:206
本文介绍了用的.htaccess重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网址: http://www.test.com/page。 PHP的?K = m1ns

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

我的.htaccess:

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /

重写规则^ķ/([^/\.]+)/?$ page.php?K = $ 1 [L]

#力WWW。在所有的请求
的RewriteCond%{HTTP_HOST} ^测试\ .COM [NC]
重写规则^(。*)$ http://www.test.com/$1 [L,R = 301]

#使隐藏php扩展
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME} \ PHP -f
重写规则^(。*)$ $ 1.PHP
 

不过,这是行不通的。只有非www - > WWW和隐藏PHP的规则运作。 如果我把 http://www.test.com/page.php?k=m1ns 不重写。

任何人都知道这是为什么?

感谢。

解决方案

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /

重写规则^ R /([^ /] *)$ /page.php?k=$1 [L]
 

在你的前page.php

 如果(的strstr($ _ SERVER ['REQUEST_URI'],/page.php?k='。$变种。'')){
    标题(HTTP / 1.1 301永久移动);
    标题(位置:http://www.test.com/r/。$ VAR);
    出口();
}
 

I've this url: http://www.test.com/page.php?k=m1ns

and I want this one: http://www.test.com/r/m1ns

My .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

But it doesn't work. Only the non-www -> www and hiding php rules works. If I put http://www.test.com/page.php?k=m1ns does not rewrite.

Anyone knows why?

Thanks.

解决方案

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

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

On your top page.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();
}

这篇关于用的.htaccess重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆