Rewrite URL with .htaccess

爱⌒轻易说出口 提交于 2019-12-31 04:39:24

问题


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.


回答1:


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();
}



回答2:


Try this

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

It should work regardless of whether or not www is entered.



来源:https://stackoverflow.com/questions/4527628/rewrite-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!