如何获取php/apache配置中域名后面的URL参数?
How can I get the URL parameter after the domain name in php/apache configuration?
例如,对于我拥有的网站 website.com/abcd,我需要提取 'abcd' 变量并存储在数据库中。如果用户访问网站。com/abcd 它会重定向到 404 页面。我一直在修补 .htaccess,但一无所获。
将以下内容弹出到 .htaccess 文件中...
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule . /index.php [L]
</IfModule>
...然后使用 <?php print_r($_SERVER);?>
显示您的服务器允许您使用的变量。
上面的 htaccess 代码非常基础;如果请求的 file/directory 实际不存在,请求将被重定向到 ./index.php
.
将自定义错误处理程序而不是内置添加到您的 .htaccess
:
ErrorDocument 404 /handle_404.php
在 handle_404.php 中,您可以访问所有需要的信息:
<?php
header("HTTP/1.0 404 Not Found");
echo "The url you requested (${_SERVER['REQUEST_URI']}), does not exist.";
// do whatever else you want (e.g. persist to DB)
例如,对于我拥有的网站 website.com/abcd,我需要提取 'abcd' 变量并存储在数据库中。如果用户访问网站。com/abcd 它会重定向到 404 页面。我一直在修补 .htaccess,但一无所获。
将以下内容弹出到 .htaccess 文件中...
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule . /index.php [L]
</IfModule>
...然后使用 <?php print_r($_SERVER);?>
显示您的服务器允许您使用的变量。
上面的 htaccess 代码非常基础;如果请求的 file/directory 实际不存在,请求将被重定向到 ./index.php
.
将自定义错误处理程序而不是内置添加到您的 .htaccess
:
ErrorDocument 404 /handle_404.php
在 handle_404.php 中,您可以访问所有需要的信息:
<?php
header("HTTP/1.0 404 Not Found");
echo "The url you requested (${_SERVER['REQUEST_URI']}), does not exist.";
// do whatever else you want (e.g. persist to DB)