【问题标题】:hide.php files with variables htaccess带有变量 htaccess 的 hide.php 文件
【发布时间】:2014-09-15 01:48:18
【问题描述】:

我的 htaccess 文件有问题,我正在尝试隐藏我网站上的所有 .php 扩展名,
我尝试了许多 htaccess 代码,但大部分都给了我错误,404 Not Found,我的大多数文件都包含变量,如 ?id 和 ?category , index/?cat=Action ,我希望 htaccess 隐藏 php 扩展名并转发 /dir/foo到 /dir/foo.php ,请帮帮我:)

RewriteEngine On
RewriteBase /

# turn off index.php for home page
RewriteRule ^index\.php/?$ / [L,R=301,NC]


# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

【问题讨论】:

  • 错误的做法。您必须检测您的“无 .php 扩展名”URL,并将它们重新写入 .php 文件。例如"检测/foo/bar,重写为/foo/bar.php"
  • 我想更改 ?id = /id
  • 是的,但您需要在客户端上有友好的网址。例如用户的浏览器应该显示/id。那么你的重写将RewriteRule ^(\d+) whatever.php?id=$1如果你开始向客户端发送“丑陋”的url,那么所有这些都是毫无意义的。

标签: php .htaccess mod-rewrite redirect hide


【解决方案1】:

您可以在您的根.htaccess 中使用此代码:

RewriteEngine On
RewriteBase /

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

【讨论】:

  • 我想更改 ?id = /id
  • 你在问题​​中清楚地写了这个要求吗?如果没有通过编辑您的问题来澄清所有这些情况,我会尽力回答。
【解决方案2】:

您需要将 php 写到 url 的末尾,而不是相反。你可以这样写变量:href="foo?bar=1"。这段代码对我有用:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 2019-08-20
    • 2015-03-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多