简体   繁体   中英

Remove Characters from web url

I have php web script that we use for all of our user accounts we have. 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

username is their account's username they used. 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

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?

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. None of the images display properly and none of the html displays correctly.

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
    $url = "http://domain.com/?username";
    $new_url = str_replace("?", "", $url);
?>

This replaces all instances of ? with nothing in url .

You can do this with the help of htaccess url rewrite rules. Use the following code to solve your purpose.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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