Question

I have my site: www.mysite.com/

And I want to get the last part of the url to enter profile: www.mysite.com/profile/NAME

How i can get NAME ( in PHP ), and where the code is run, because it would take NAME as a folder.

Was it helpful?

Solution

Here is what is in my .htaccess file and it works in prod

It takes a name such as www.website.com/userprofile and puts in a link named www.website.com/webpage.php?u=userprofile

# PROFILER
RewriteEngine On
# force non-www domain
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule (.*) http://website.com/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([\w_|-]+)$ webpage.php?u=$1 [L,QSA]
RewriteRule ^([\w_|-]+)\/$ webpage.php?u=$1 [L,QSA]

OTHER TIPS

Your question seems a bit unclear. To access parts of the url for operations:

Check out the super global $_SERVER[]. It has various options that may be useful to you. Specifically:

'PHP_SELF' The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address www.example.com/foo/bar.php would be /foo/bar.php. The FILE constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.

http://www.php.net/manual/en/reserved.variables.server.php

As it says, the FILE constant may be of help, or you could use:

explode("/", $_SERVER['PHP_SELF']);

which will give a hash of:

{0 => "foo", 1 => "bar.php"}

Be careful here though. If you allow arbitrary queries and use those queries to access files on your server, you could be introducing serious security issues.

If instead, you mean you want the url to display as you've written:

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top