.htaccess重写文件夹路径的文件路径

时间:2017-04-11 09:43:57

标签: php apache .htaccess mod-rewrite url-rewriting

我有一个子目录联系人,里面有contact.php和contact_content.php和.htaccess文件

在.htaccess文件中,我有以下代码:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/contact/contact.php
RewriteRule ^ /contact [R=301,L]

RewriteRule ^/$ contact.php [NC,L,END] 

# deny access
RewriteRule ^(contact_content)\.php - [F,L,NC]

拒绝访问部分和第一个重写规则完美地工作但是 问题是第二次重写 - >当我转到localhost/contact时,我会看到联系人目录中的文件列表,而不是contact.php网站

2 个答案:

答案 0 :(得分:2)

这适合我 - >修改Sahil Gulati回答

Options +FollowSymLinks -Indexes
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/contact/contact.php
RewriteRule ^ /contact [R=301,L]

RewriteCond %{REQUEST_URI} ^/contact/?$
RewriteRule ^(.*)$ /contact/contact.php [L,QSA,END]

RewriteRule ^(contact_content)\.php - [F,L,NC]

答案 1 :(得分:1)

希望它能正常工作。

RewriteEngine On

Options -Indexes
RewriteCond %{REQUEST_URI} contact\.php$
RewriteRule ^(.*)$ /contact [R=301]


RewriteCond %{REQUEST_URI} ^/contact/?$
RewriteRule ^(.*)$ /$1/contact.php [L,QSA,END]

RewriteRule ^(contact_content)\.php - [F,L,NC]