【发布时间】:2020-11-01 14:19:42
【问题描述】:
我正在做用户控制面板,但我有一些问题我目前不知道如何做这个动态配置文件链接。 example.com/profile/CroatiaGM 最好的方法是什么,我的网站主要是 PHP,使用 js 更好还是?
【问题讨论】:
标签: javascript php
我正在做用户控制面板,但我有一些问题我目前不知道如何做这个动态配置文件链接。 example.com/profile/CroatiaGM 最好的方法是什么,我的网站主要是 PHP,使用 js 更好还是?
【问题讨论】:
标签: javascript php
我假设您要重写一个 URL,您希望将 example.com/profile.php?user=CroatiaGM 重写为 example.com/profile/CroatiaGM。
在这种情况下,如果您还没有 .htaccess 文件,我相信这会有所帮助,我建议您在主目录/public_html 中创建一个
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)?$ profile.php?username=$1
RewriteRule ^profile/([^/]+)/([^/]+)?$ profile.php?username=$1&type=$2
</IfModule>
或
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?username=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ $1.php?username=$2&type=$3
</IfModule>
这是一个工作示例。 https://locationtestproject.000webhostapp.com/test/myname
我强烈建议在保存任何更改之前备份 .htaccess。
https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file- How to use .htaccess for beautiful links https://docs.bolt.cm/4.0/howto/making-sure-htaccess-works https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
【讨论】:
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $baseurl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $pos = strrpos($url, '/'); $profilename = $pos === false ? $url : substr($url, $pos + 1); if ($pos === 32) { }