如何删除.php扩展名并在网址上添加斜杠?

时间:2011-02-01 02:20:09

标签: .htaccess url-rewriting

这是我当前的.htaccess代码

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

它仅适用于删除.php扩展名

http://localhost/mysite/news.php?category=cat1&id=1http://localhost/mysite/news/cat1/1/

http://localhost/mysite/news.php?category=cat1&year=2011&month=10&day=25&id=1http://localhost/mysite/news/2011/10/25/1

如何为上面的干净网址写完整的.htaccess?

3 个答案:

答案 0 :(得分:3)

试试这个

RewriteRule ^(。*)\ / $ $ 1.php [NC]

例如

  

http://www.exapmle.com/contact-us/

答案 1 :(得分:2)

为您的模式添加一个/:

RewriteEngine On

# rewrite news articles and pass news id as a GET parameter to news.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/news/(\d+)/?$ /mysite/news.php?newsid=$1 [NC,L,QSA]

# rewrite all other page requests to .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/(.*)/?$ /mysite/$1.php [NC,L,QSA]

答案 2 :(得分:1)

您可以尝试一个简单的RewriteRule

RewriteEngine on
RewriteRule ^/?(\w+)/?$ $1.php

对于任何看起来像文件的网址.phpexample.com/somethin会变为example.com/somethin.phpexample.com/something/else/变为example.com/somethin/else.php等等。

唯一的问题是,如果您尝试访问实际文件夹,例如example.com/images或其他内容。