删除文件扩展名并使用htaccess重写URL

时间:2016-08-18 03:06:22

标签: apache .htaccess

我正在尝试

  1. www.website.com/stuff.php至www.website.com/stuff
  2. www.website.com/things/stuff.php至wwww.website.com/things/stuff
  3. www.website.com/news.php?article=1至wwww.website.com/news/1
  4. 目前我的htaccess如下,但它只允许方案1&我尝试了其他尝试,其中3个工作,但不是1&我无法弄清楚如何让多种条件共存。

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^news/([^/]*)$ /news.php?article=$1 [L]
    

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则

RewriteEngine on

 #remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
 RewriteRule ^(.*?)/?$ /$1.php [L]
 #news/1 to /news.php?article=1
 RewriteRule ^news/(.+)$ /news.php?article=$1 [QSA,L]