【问题标题】:How do you do dynamic url. example.com/profile/CroatiaGM你如何做动态网址。 example.com/profile/CroatiaGM
【发布时间】:2020-11-01 14:19:42
【问题描述】:

我正在做用户控制面板,但我有一些问题我目前不知道如何做这个动态配置文件链接。 example.com/profile/CroatiaGM 最好的方法是什么,我的网站主要是 PHP,使用 js 更好还是?

【问题讨论】:

    标签: javascript php


    【解决方案1】:

    我假设您要重写一个 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']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $baseurl = (isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $pos = strrpos($url, '/'); $profilename = $pos === false ? $url : substr($url, $pos + 1); if ($pos === 32) { }
    • 那么你会怎么做 somesite.com/profile/CroatiaGM/pictures 你会去 example.com/profile.php?user=CroatiaGM&pictures 还是?
    • 为简单起见,您必须执行类似 example.com/profile.php?user=CroatiaGM&type=pictures 的操作
    • 如何在 .htaccess 文件中进行操作?使用 example.com/profile.php?user=CroatiaGM&type=pictures
    • 类似这样的东西。 RewriteRule ^test/([^/]+)/([^/]+)?$ test.php?username=$1&type=$2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2011-06-25
    • 2022-06-29
    相关资源
    最近更新 更多