Url 用 .htaccess 和 404 原始路径重写
Url Rewriting with .htaccess and 404 original path
我有一个文件 test.php 需要重写 test.json
在 .htaccess 中我设置了
RewriteRule ^test.json$ /test.php
重写工作正常,但如果我打开旧的 url、test.php,
我仍然可以访问。对于原始 url 我会出现 404 错误。
您想要的是添加额外的规则来处理您的 test.php
。以下是您可能最终得到的示例:
在您想要 "server" 您的 test.json:
目录中的 .htaccess 文件中
RewriteEngine on
RewriteRule ^test.json$ /test.php [L,END]
在网络根目录的 .htaccess 文件中:
RewriteEngine on
RewriteRule ^test.php$ - [L,R=404]
或者,如果您希望它们都在同一个目录中,可以将它们全部放在一个 .htaccess 文件中:
RewriteEngine on
RewriteRule ^test.json$ /test.php [L,END]
RewriteRule ^test.php$ - [L,R=404]
请注意,需要 END
标志来阻止 apache 执行另一个循环并将 test.php 重写为 404 错误。
我有一个文件 test.php 需要重写 test.json
在 .htaccess 中我设置了
RewriteRule ^test.json$ /test.php
重写工作正常,但如果我打开旧的 url、test.php, 我仍然可以访问。对于原始 url 我会出现 404 错误。
您想要的是添加额外的规则来处理您的 test.php
。以下是您可能最终得到的示例:
在您想要 "server" 您的 test.json:
目录中的 .htaccess 文件中RewriteEngine on
RewriteRule ^test.json$ /test.php [L,END]
在网络根目录的 .htaccess 文件中:
RewriteEngine on
RewriteRule ^test.php$ - [L,R=404]
或者,如果您希望它们都在同一个目录中,可以将它们全部放在一个 .htaccess 文件中:
RewriteEngine on
RewriteRule ^test.json$ /test.php [L,END]
RewriteRule ^test.php$ - [L,R=404]
请注意,需要 END
标志来阻止 apache 执行另一个循环并将 test.php 重写为 404 错误。