Twitter Like URL, but with @?

I am making a social networking site and I can’t do this: If url is http://url.com/@username , I want it to show the page http://url.com/user?u=username but show the URL http://url.com/@username .

However, if there is no @ at the beginning, use it as a regular URL.

This is my .htaccess file:

 Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L] RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^/(\d+)*$ ./user.php?u=$1 

The last three lines are what I tried, however this does not work as I wanted it. I thought this would work because RewriteRule ^/(\d+)*$ takes any URI after the slash and overwrites it with .user.php?u=* , but that didn't work, so I'm looking for some suggestions about what to do next.

+4
source share
1 answer

From my understanding of your question, this should work.

 Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^@(.+?)$ user.php?u=$1 [NC,L] 

This requires that you have @ in front of the username and it is passed as the variable GET (u). However, a person may add other characters that may confuse him as a username.

/ @ username / cupcakes

But if you want it and have pages for the username (i.e../@ username / info, / @ username / o, etc.), you can just use explode () in PHP.

If you just want it to get a username and nothing else, you can try

 RewriteRule ^@([A-Za-z0-9]+)$ user.php?u=$1 [NC,L] 

Hope this helps! :)

+3
source

Source: https://habr.com/ru/post/1497697/


All Articles