如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test?

时间:2016-08-23 10:33:31

标签: php apache .htaccess mod-rewrite url-rewriting

我有一个默认网址http://example.com/test.php
我想将此网址设为http://example.com/index.php?do=test


http://example.com/test.phphttp://example.com/index.php?do=test

我使用下面的代码,但没有工作......

RewriteEngine On
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L]

提前致谢。

2 个答案:

答案 0 :(得分:1)

这样可以解决问题:

RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f

答案 1 :(得分:0)

使用以下

.htaccess文件

RewriteEngine On
RewriteRule ^(.+)$ index.php?do=$1 [QSA,L]

的index.php

<?php 
if(isset($_GET['do']) && !empty($_GET['do'])){
    include $_GET['do'] . '.php';
} else {
    //include 'index.php';
    //You can include your default file or Error page here
}
?>

test.php的

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    Hello You are in Test Page
</body>
</html>

网址:http://localhost/index.php?do=test