【问题标题】:Remove Characters from web url从网址中删除字符
【发布时间】:2013-08-22 18:00:25
【问题描述】:

我有用于我们所有用户帐户的 php web 脚本。我们每个拥有帐户的用户都有一个可以使用的复制网站。该脚本会像这样自动生成他们复制的网站 url。

http://domain.com/?username

username 是他们使用的帐户用户名。我想知道是否可以从 url 中删除问号,以便他们可以使用类似的 url 访问他们的复制站点。

http://domain.com/username

我只想简单地从网址中删除问号。这可能吗?最好的方法是什么?我可以用 .htaccess 做这样的事情吗?

编辑:

我尝试将其添加到我的 .htaccess 文件中,但当我转到 http://domain.com/?username 之类的网址时,它不会像我想要的那样正确删除问题,但页面已损坏。没有一个图像正确显示,没有一个 html 显示正确。

RewriteEngine On

# This is to physically change what's in the browser's address bar using a client redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+)
RewriteRule ^$ /%1? [R=301,L]

# This is to internally rewrite on the server side
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.+)$ /?$1 [L]

谢谢。

【问题讨论】:

  • 是的,看看重写规则。
  • 你想如何处理:http://domain.com/?q=abc&u=name&id=1123 类型的 URL?它应该变成:http://domain.com/q=abc&u=name&id=1123 没有?
  • 是的,这正是它应该成为的样子。我真正想要的是让用户能够通过输入domain.com/username 来访问domain.com/?username

标签: php .htaccess url


【解决方案1】:

可以使用php的str_replace函数:

<?php
    $url = "http://domain.com/?username";
    $new_url = str_replace("?", "", $url);
?>

这会将? 的所有实例替换为url 中的任何内容。

【讨论】:

  • 谢谢,但我实际上并不知道网址。我们有数百个用户帐户,因此用户名可以是动态更改的任何内容。
【解决方案2】:

您可以在 htaccess url 重写规则的帮助下做到这一点。 使用以下代码来解决您的目的。

RewriteEngine on #turn on rewrite engine
RewriteRule /username/(.*)/username ?username=$1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多