htaccess mod_rewrite - 将特定的多页直接指向一个页面,并拒绝访问带扩展名的文件

时间:2015-07-01 05:28:04

标签: regex apache .htaccess mod-rewrite

我需要你的帮助来编写htaccess文件,以便1)将具有下面的网址的特定页面重定向到一个页面,2)还拒绝访问扩展名为php的页面。

1)
www.example.com/index   --> www.example.com
www.example.com/about   --> www.example.com
www.example.com/contact --> www.example.com

2)
not found

www.example.com/index.php
www.example.com/about.php
www.example.com/contact.php


3)
be found 

www.example.com/index
www.example.com/admin

感谢 拉敏

1 个答案:

答案 0 :(得分:0)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On

RewriteRule ^(index|about|contact)/?$ / [NC,L,R=302]

RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule \.php$ - [L,R=404]

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /dolarfori/index.php?path=$1 [NC,L,QSA]