【问题标题】:Htaccess, rewrite and show 404Htaccess,重写并显示 404
【发布时间】:2015-10-28 11:22:19
【问题描述】:

抱歉,如果这是重复的,可能是我使用了错误的关键字,但我无法找到解决方案。

我的问题是如何(使用 Apache;.htaccess)实现以下目标:

我有一个名为 show_results.php 的文件,我想: 将 show_results.php 重写为 /results,当有人试图访问 show_results.php 时会带来 404 错误(但 /results 必须正常工作)。

我尝试了许多不同的方法,但都没有成功 - 当我重写时,我能够访问原始页面。当我尝试将原始页面重定向到 404 页面时,“重写”页面也显示 404。

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    RewriteEngine On
    RewriteBase /
    
    # To return 404 for any/dir/foo.php
    RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC]
    RewriteRule ^ -[R=404,L]
    
    ## To internally redirect /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    【讨论】:

    • 抱歉回复晚了。我想将原始页面重定向到本机 404 错误?这可能吗?
    • 对不起。我会更好地解释。这正是我想要发生的事情——当我访问 /results 以打开 show_results.php 时,当我直接访问 show_results.php 导致 404 错误(需要使用纯 htacess 完成)时。谢谢!!
    猜你喜欢
    • 2013-08-03
    • 2012-11-11
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多