I'm working on a website with a lot of profiles. What I want is that you can simply navigate to one of those profiles with a URL like www.mywebsite.com/userX (just like twitter with twitter.com/userX).
I already know how this is possible by using the .htaccess and rewriterule, but the problem is it changes the entirely URL to something like www.mywebsite.com/?username=userX. It must just load a specific PHP-file without rewriting the URL in the addressbar.
Who knows how I can perform this?
(p.s. sorry for my average English)
See the fine article:
http://www.phpriot.com/articles/search-engine-urls
It explains that yeah, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
Related
I have created the website like WOWcrush http://phone77.ml I want the url be like phone77.ml/Naveen in title it should have my name but there come ? at the last of the URl like http://phone77.ml/?Naveen only if use ? it works fine or it shows 404 error please help
You will need to use the .htaccess file in order to point /Naveen to the coresponding get var.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]*)/?$ index.php?section=$1 [L]
or you can be more specific like this
RewriteRule ^/Naveen$ index.php?section=Naveen [L]
$_GET['section'] will contain Naveen and you can use that in your code to show the correct view
By default everything what goes after the '?' symbol is parameters in url. It can be changed, but for that is necessary to properly configure a web server (e.g. Apache or Nginx).
Also can be necessare to made some changes in code or/and configuration of your application.
You can see the question mark as a 'where'. So what you are saying is;
http://phone77.ml/ 'WHERE' Naveen. It all depends on how you build your site, but apparently you have a clause looking for Naveen in the URL. Maybe something like; if(isset($_GET['Naveen'])){...}.
You could also have separate documents for each you pages, then you would link to to that document instead and the URL would look like http://phone77.ml/Naveen.php f.ex.
You might also be interested in looking up the .htaccess document https://httpd.apache.org/docs/current/howto/htaccess.html where you can manipulate the URL from.
Sorry if the title doesn't sound clear but basically, I see multiple APIs doing it like this:
http://www.website.com/183718
Where 183718 would be the ID and then they could use that to pull results.
I want to do the same but I don't know another way besides like this:
http://www.website.com/id?=183718
How could I accomplish this?
You want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
I have the following htaccess file:
Rewriterule ^view_profile-(.*)$ view.php?user=$1
When I do something like "View profile it works great (URL looks like: http://home.com/view_profile-John). However, if I try this:
Rewriterule ^view/profile-(.*)$ view.php?id=$1 it says that page cannot be found.
Does anyone know why is not working?
You probably need to turn off Multiviews here. Because the beginning of the request looks like /view/... and there's a file /view.php, mod_negotiation will automatically assume you mean the php file and route it there before mod_rewrite even gets a chance to do anything. Try adding:
Options -Multiviews
I am seeing this url format at most websites.
site.com/extension/rar
I wonder how they get the value='rar' using $_GET.
What I know is that $_GET can be use in here
site.com/extension/index.php?ext=rar
Now I wanted to change my way of calling a variable.
I wanted to apply what most websites do.
How can I call variable in the former?
Perhaps this works to get the "rar":
$name = basename($_SERVER['REQUEST_URI']);
I most likely being done using .htaccess
It is an Apache module that allows you "rewrite" urls at the engine level based on your own set of rules. So basically it rewrites URLs on the fly.
So, in your example you could have a file named .htaccess with the following contents: (there may be other options)
RewriteEngine On
RewriteRule ^extension/([a-z0-9]+)$ somefile.php?extension=$1 [L]
Basically, you are saying: If someone is looking for a URL that looks like "extension/somenumbers-and-letters" then show the contents of "somefile.php?extension=whatever-those-number-and-leters-are".
Do a search on Apache mod_rewrite to find more information.
Let' say for example one of my members is to http://www.example.com/members/893674.php. How do I let them customize there url so it can be for example, http://www.example.com/myname
I guess what I want is for my members to have there own customized url. Is there a better way to do this by reorganizing my files.
You could use a Front Controller, it's a common solution for making custom URLs and is used in all languages, not just PHP. Here's a guide: http://www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html
Essentially you would create an index.php file that is called for every URL, its job is to parse the URL and determine which code to run base on the URL's contents. So, on your site your URLs would be something like: http://www.example.com/index.php/myname or http://www.example.com/index.php/about-us or http://www.example.com/index.php/contact-us and so on. index.php is called for ALL URLs.
You can remove index.php from the URL using mod_rewrite, see here: http://www.wil-linssen.com/expressionengine-removing-indexphp/
Add a re-write rule to point everything to index.php. Then inside of your index.php, parse the url and grab myname. Lookup a path to myname in somekinda table and include that path
RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]
index.php:
$myname = $_SERVER['REQUEST_URI'];
$myname = ltrim($myname, '/'); //strip leading slash if need be.
$realpath = LookupNameToPath($myname);
include($realpath);
create a new file and change it name to (.htaccess) and put this apache commands (just for example) into it :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^profile/([0-9]*)$ members.php?id=$1
You must create a rewrite rule that point from http://www.example.com/myname to something like http://www.example.com/user.php?uname=myname.
In '.htaccess':
RewriteEngine on
RewriteRule ^/(.*)$ /user.php?uname=$1
# SourceURL TargetURL
Then you create a 'user.php', that load user information from 'uname' GET variable.
See from your question, you may already have user page based on user id (i.e., '893674.php') so you make redirect it there.
But I do not suggest it as redirect will change the URL on the location bar.
Another way (if you already have '893674.php') is to include it.
The best way though, is to show the information of the user (or whatever you do with it) right in that page.
For example:
<?phg
vat $UName = $_GET['uname'];
var $User = new User($UName);
$User->showInfo();
?>
you need apache’s mod_rewrite for that. with php alone you won’t have any luck.
see: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html