0

I have the URL of www.domain.com/profile/user123 (where user123 will vary for each user profile viewed).

How can I get 'user123' into a variable in PHP? I just need to get the very last part of the URL. I don't want to use ? as part of the URL, but right now I'm getting a 404 because the URL doesn't exist.

I'm not sure if I need to modify the .htaccess file? The only modification I have in it so far is to remove .php requirement in URL.

gee-ell
  • 11
  • 2
    Does this answer your question? [.htaccess rewrite "/book.php?id=1234" to "/book/1234"](https://stackoverflow.com/questions/11696718/htaccess-rewrite-book-phpid-1234-to-book-1234) – Progman Oct 26 '20 at 20:37
  • Welcome we're glad to have you on StackOverflow! To get the most out of your questions please provide as much information as you can regarding what you are wanting to accomplish and what you have tried so far. Specifically: Please provide additional information about ".../user123". Is that a php file named user123.php? Is it intended to be a directory? (a directory is what a browser expects) Is there some reason that you don't want to use URL parameters (the parts of a URL that are after the ? ... and should be key value pairs)?? Please tell us more! – MER Oct 26 '20 at 21:47

2 Answers2

1

Can you try this. I edited it the fist answer was wrong. Now it is right.

<?php 
$url="www.domain.com/profile/user123";
$arr=explode("/",$url);
$userVariable=end($arr);
?>

you can use $userVariable in anywhere on your php document. After triying can you write your result?

Filayer
  • 51
  • 4
  • This doesn't work for me, as it still shows the page as a 404. – gee-ell Oct 27 '20 at 21:16
  • Before using this code you have modify your htaccess. This code works if the URL is accessible. Than it may help you. – Filayer Oct 27 '20 at 21:20
  • Filayer - what modification should I make to the .htaccess file? – gee-ell Oct 28 '20 at 21:08
  • can you share your .htaccess file – Filayer Oct 29 '20 at 00:47
  • Sure - # Run Php without filename extension RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php # Return 404 if original request is .php RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" RewriteRule .* - [L,R=404] ErrorDocument 400 /400.php ErrorDocument 401 /401.php ErrorDocument 403 /403.php ErrorDocument 404 /404.php ErrorDocument 500 /500.php – gee-ell Oct 31 '20 at 00:00
  • Sorry @Filayer I realise it wasn't marked up correctly. `code # Run Php without filename extension RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php # Return 404 if original request is .php RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" RewriteRule .* - [L,R=404] ErrorDocument 400 /400.php ErrorDocument 401 /401.php ErrorDocument 403 /403.php ErrorDocument 404 /404.php ErrorDocument 500 /500.php ` – gee-ell Nov 04 '20 at 20:36
0

if($usr == 'user123') header ('location: www.domain.com/profile/''');

  • 1
    Please see [answer] and [How do I format my code blocks?](https://meta.stackoverflow.com/q/251361/4642212) and [edit] your post. An answer with an explanation and additional context is more useful than a [code-only answer](https://meta.stackoverflow.com/a/298811/4642212). For example, how exactly does this code solve the OPs problem? – Sebastian Simon Oct 26 '20 at 22:51