简体   繁体   English

从网址中删除字符

[英]Remove Characters from web url

I have php web script that we use for all of our user accounts we have. 我拥有用于​​所有用户帐户的php网络脚本。 Each one of our users who have an account have a replicated website which they can use. 我们每个拥有帐户的用户都有一个可复制的网站。 The script automatically generate their replicated website url like this. 该脚本将自动生成其复制的网站网址,如下所示。

http://domain.com/?username http://domain.com/?用户名

username is their account's username they used. username是他们使用的帐户的用户名。 I was wondering if it was possible to remove the question mark from the url so they can access their replicated site with a url like instead. 我想知道是否有可能从网址中删除问号,以便他们可以使用类似的网址访问其复制的站点。

http://domain.com/username http://domain.com/username

I just want to simply remove the question mark from the urls. 我只想简单地从网址中删除问号。 Is this possible? 这可能吗? What would be the best way to do it? 最好的方法是什么? Could I do something like this with .htaccess? 我可以用.htaccess做这样的事情吗?

EDIT: 编辑:

I tried adding this to my .htaccess file, but not when I go urls like http://domain.com/?username it removes the question properly like I'd want to, but the page is broken. 我尝试将其添加到我的.htaccess文件中,但是当我访问诸如http://domain.com/?username之类的网址时,它并没有像我想要的那样正确地删除了问题,但是页面已损坏。 None of the images display properly and none of the html displays correctly. 没有图像正确显示,没有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]

Thanks. 谢谢。

You can use php's str_replace function: 您可以使用php的str_replace函数:

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

This replaces all instances of ? 这将替换?所有实例? with nothing in url . url没有任何内容。

You can do this with the help of htaccess url rewrite rules. 您可以借助htaccess网址重写规则来执行此操作。 Use the following code to solve your purpose. 使用以下代码可以解决您的目的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM