0

I'm a beginner in php and primarily with htaccess part. It's pretty darn confusing.

So I have this file :-

www.domain.com/profile.php?username=my_username

How can I make the above accessible using only :-

wwww.domain.com/my_username

Any kind of directions would be helpful

Something that I tried :-

RewriteEngine on
RewriteRule ^/([0-9]+)$ /profile.php?username=$1
anubhava
  • 761,203
  • 64
  • 569
  • 643
xanderpower
  • 49
  • 1
  • 7

3 Answers3

2

You can use these set of rule in your site root .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /foldername/

RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

you can achieve this by .htaccess

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/(\d+)*$ ./profile.php?username=$1
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
0

The .htaccess file has to be given permission by apache to modify/override settings, depending on your host this might not be enabled by default.

As others have mentioned .htaccess isn't part of PHP it's a file that tells apache how to deal with certain request and what parts of the server are allow to be accessed along with other functionality.

You can achieve your desired result with rewritecond just make sure the Rewrite Engine is enabled.

Here is some more information about .htaccess https://code.tutsplus.com/tutorials/the-ultimate-guide-to-htaccess-files--net-4757

Orlando P.
  • 621
  • 3
  • 19