如何使用.htaccess基于此条件重定向?

时间:2012-12-15 14:15:27

标签: php .htaccess rewrite

目前我使用以下.htaccess删除.php扩展名

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

我想在输入www.example.com/login.php时显示页面www.example.com/login,并希望在输入www.example.com/profile=username时显示www.example.com/username

提前感谢任何建议或答案

2 个答案:

答案 0 :(得分:2)

http://www.9lessons.info/2009/11/pretty-urls-with-htaccess-file.html

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1

<?php

$key=$_GET['key'];

if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
} 
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else 
{
include('users.php'); // Users Gateway
}
?>

答案 1 :(得分:1)

这是另一种选择:

RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)/([\w]+)\.php [NC]

#Select ONE of the following options and DELETE the others
# 1) Without redirection
RewriteRule .*  %1/%2 [L]

# 2) With temporary redirection
RewriteRule .*  %1/%2 [R,L]

# 3) With permanent redirection
RewriteRule .*  %1/%2 [R=301,L]

相应地选择重写规则。