【问题标题】:.htaccess rewrite file path to folder path.htaccess 将文件路径重写为文件夹路径
【发布时间】:2017-04-11 09:43:57
【问题描述】:

我有一个子目录contact,里面有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 站点

【问题讨论】:

  • 希望我当前的帖子能正常工作。我已经在您所指的所有案例中进行了测试
  • 部分完成了,我会放上现在适合我的代码

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


【解决方案1】:

这对我有用->修改后的 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]

【讨论】:

    【解决方案2】:

    希望它能正常工作。

    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]
    

    【讨论】:

    • 那么当用户输入/contact时你想要什么
    • 所以当用户输入 /contact 我想显示contact.php页面
    • @MPavic 你想在用户点击/contactcontact_content.php 时执行/contact/contact.php 你想禁止吗?
    • 是的,这是正确的-> 当前的禁止效果很好,如果用户直接转到 /contact/contact.php,即使重定向也有效,它会将他重定向到 /contact
    猜你喜欢
    • 2013-08-12
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2018-04-05
    • 2016-07-20
    相关资源
    最近更新 更多