将example.com/dashboard.php更改为example.com/index.php?action=dashboard

时间:2014-03-02 05:16:10

标签: php mod-rewrite

我想更改我的网站页面网址 以下内容:

mysite.com/profile.php

mysite.com/index.php?action=profile

什么语言可以用于此?我尝试了一堆.htacesss代码,但似乎都没有用!有.htacess个建议吗?

我的代码:

RewriteEngine On
RewriteRule   ^profile.php(.+)$   /index.php?action=$1   [L]

2 个答案:

答案 0 :(得分:0)

为什么不直接在index.php上放置重定向代码,这样如果$ _GET ['action']变量存在并指向'profile',你将被重定向到profile.php?

否则我相信.htaccess是你将要用来解决这个重定向/ URL重写的问题。

将它放在index.php页面的顶部...

<?php

    if ($_GET['action'] == "profile") {
        echo "<meta http-equiv='refresh' content='0; url=profile.php' />";
    }

?>

另外,请检查一下。 http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/

答案 1 :(得分:0)

使用htaccess重定向是另一种方式,当我们想要有干净的URL'时,并没有看到很多其他方式,但它当然是可能的。我同意@ScottCarmichael,如果你正在使用查询字符串PHP如果语句(或开关)是一个很好的解决方案(但我优先使用hearder()函数而不是刷新),那么你可以在顶部有一个包含包含所有条件的PHP脚本。

如果您仍然认为htaccess适合您,请尝试以下方法:

RewriteEngine on
RewriteRule ^profile.php$ index.php?action=profile [R,L]
相关问题