使用htaccess从url中删除.php

时间:2012-12-25 23:22:56

标签: .htaccess

我需要知道如何从目录索引文件中删除“.php”

我不想看到这个:

http://www.domain.nl/wa.admin/index.php?login_msg=nopassword

我看了很多帖子,但由于某些原因它们对我不起作用。

这些是我的基本规则

RewriteRule ^admin[/]*$ /wa.admin [L]

RewriteRule ^([^./]{2}[^.]*)$ /index.php?page=$1 [QSA,L]

我必须在内部做什么,因为它来自重定向? 我是否必须使用绝对网址才能使其正常工作?

谢谢,Rich

1 个答案:

答案 0 :(得分:1)

这应该适合你。

RewriteBase /

# Stop rewrite process if the path points to a static file anyway
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L]

RewriteRule (.*) index.php

用文字解释:如果请求的资源是文件夹或文件不重写,如果没有重写到index.php。

在您的情况下,wa.admin/index.php?login_msg=nopassword看起来像wa.admin/?login_msg=nopassword

不要忘记更新您的申请。此规则仅将请求映射到适当的文件。这不会影响HTML输出。这意味着通过此重写,您可以访问这两个URL,但这取决于您要在应用程序中链接的内容。

如果您想与其他人分发您的应用,您可以将环境与if标签结合使用,以确定mod_rewrite是否可用。

<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On

# Set flag so we know URL rewriting is available
SetEnv REWRITEURLS 1

# You will have to change the path in the following option if you
# experience problems while your installation is located in a subdirectory
# of the website root.
RewriteBase /

# Stop rewrite process if the path points to a static file anyway
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L]

RewriteRule (.*) index.php

</IfModule>

使用apache_getenv("REWRITEURLS");来获取set变量的值。

您的代码可能如下所示(未经测试):

<?php
$rewriteurls = apache_getenv("REWRITEURLS");

if ($rewriteurls == '1') {
    //adjust all your links
}
?>