0

I am doing the user control panel, and I have some problems I am not sure how to do this dynamic profile link currently. example.com/profile/CroatiaGM What would be the best way to go about this, My site is mainly PHP, is it better to use js or?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
CroatiaGM
  • 5
  • 2

1 Answers1

1

I'm assuming you're looking to rewrite a URL, where you'd like to rewrite example.com/profile.php?user=CroatiaGM to example.com/profile/CroatiaGM.

in such an instance I believe this would be helpful if you don't already have a .htaccess file, I suggest you create one in your main directory/public_html

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)?$ profile.php?username=$1
RewriteRule ^profile/([^/]+)/([^/]+)?$ profile.php?username=$1&type=$2
</IfModule>

or

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?username=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ $1.php?username=$2&type=$3
</IfModule>

Here's a working example. https://locationtestproject.000webhostapp.com/test/myname

I strongly advise making a back of the .htaccess before saving any changes.

https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file- How to use .htaccess for beautiful links https://docs.bolt.cm/4.0/howto/making-sure-htaccess-works https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

  • So this way i am doing is very wrong, right? `$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $baseurl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $pos = strrpos($url, '/'); $profilename = $pos === false ? $url : substr($url, $pos + 1);` if ($pos === 32) { } – CroatiaGM Nov 01 '20 at 14:43
  • so how would you go about doing somesite.com/profile/CroatiaGM/pictures would you just go example.com/profile.php?user=CroatiaGM&pictures or ? – CroatiaGM Nov 01 '20 at 14:47
  • for simplicity sake, you'll have to do something like this example.com/profile.php?user=CroatiaGM&type=pictures – waheed rahman Nov 01 '20 at 14:57
  • How would you do it in the .htaccess file? with example.com/profile.php?user=CroatiaGM&type=pictures – CroatiaGM Nov 01 '20 at 15:01
  • something like this. RewriteRule ^test/([^/]+)/([^/]+)?$ test.php?username=$1&type=$2 – waheed rahman Nov 01 '20 at 15:20
  • One more thing whats the diffrance between those two .ht files that you sent up there. Do you recommend one above other? – CroatiaGM Nov 01 '20 at 17:16
  • for the first one, the rewriting will only be applied to the profile.php file while the second approach will apply to all PHP files. I recommending using the first example since you have better control over what URL gets rewritten. – waheed rahman Nov 01 '20 at 17:20
  • Is that first one compatible with removing php extension like this RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] – CroatiaGM Nov 01 '20 at 17:34
  • I am not sure I am having difficulty getting this to work I put everything as you said but it still doesn't redirect, I am using windows 10 XAMPP I put allow overwrite on all and enabled mod-rewrite module this is my htacess file RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] RewriteEngine on RewriteBase / RewriteRule ^profile/([^/]+)?$ profile.php?username=$1 RewriteRule ^test/([^/]+)/([^/]+)?$ test.php?username=$1&type=$2 – CroatiaGM Nov 01 '20 at 18:00
  • I tried a simple redirect to the URL and it works but this ht file that you posted above does not work for me for some reason. – CroatiaGM Nov 01 '20 at 18:05
  • This should do the trick, RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/]+)?$ $1.php RewriteRule ^profile/([^/]+)?$ profile.php?username=$1 RewriteRule ^profile/([^/]+)/([^/]+)?$ profile.php?username=$1&type=$2 – waheed rahman Nov 01 '20 at 18:46
  • please make sure your file name is reflected in the .htaccess, change profile.php file name to your file name. – waheed rahman Nov 01 '20 at 18:48
  • yeah, I got it working anyway I need to convert it to Nginx format but everything works now thanks. – CroatiaGM Nov 01 '20 at 22:32