How To rewrite a url with PHP? - php

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

Related

php query without var=

I'd like the end user of my website to be able to do something along the lines of: example.com/user/user_name to show the statistics of that user on a different website. In short, I'd like to be able to make queries in php without having the user do: example.com/user/username=user_name I also have no way of knowing all of the possible users. I guess this means I'd like to always redirect to the index, while preserving the querystring that doesn't have "var=" in it. How would I do this?
you cna use the explode function in php. use it on the url slashes "/" and you get valid data to query. Remember to make it secure from sql injections.
EDIT
here you go:
$url = explode('/',$_SERVER['REQUEST_URI']);
echo 'user = '.$url[count($url)-1].'<br />
username = '.$url[count($url)-2];
EDIT 2
yeah you need to use .htaccess
unless you wanna get away with the questionmark
http://example.com?/user/username
EDIT 3
if you use apache you can make all traffic go through index.php with following .htaccess file:
RewriteEngine On
# always run through the indexfile
RewriteRule .$ index.php
Then if you want some folder to not go through to index.php, you add a htaccess file in that folder with:
RewriteEngine Off
The easiest way to do this is to still have the script be somepage.php?username=user_name and then have in your .httaccess file:
RewriteEngine On
RewriteRule ^/user/(.*) somepage.php?username=$1
This way you can still get the variable with $_GET['username'] but it looks nice to the user.

How to add a custom word to my domain URL for every request?

Consider my domain name is
www.mydomain.com
Consider a page request
www.mydomain.com/user/register
I want to add a custom word after base URL for every request inside mydomain.com.example
www.mydomain.com/customword/
www.mydomain.com/customword/user/register
Can we do this using URL rewriting in htaccess file ?
Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.
You could create the directory "register", and put an index file inside it that performs the action.
That's probably the simplest way without url rewriting anyway.
UPDATE (since the question was updated)
In .htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1
register.php will receive a GET request:
//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case
Make sure that you have mod_rewrite enabled :)
Yes, you can do it with htaccess
Here is an example which will add a trailing slash with url if it doesnt contain trailing slash
http://enarion.net/web/htaccess/trailing-slash/
edit formatting updated
If you are serving one site from this then the following should work:
Edit your .htaccess file to do a url rewrite
accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry
RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1
You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.

Creating dynamic URLs in htaccess

I'm trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I'm wondering how I could do this dynamically.
My URL generally looks like:
view.php?mode=prod&id=1234
What I'd like to do is take the id from the url, do a database query, then put the title returned from the DB into the url. something like:
/products/This-is-the-product-title
I know that some people have accomplished this with phpbb forum URLs and topics, and i've tried to track the code down to where it replaces the actual URL with the new title string URL, but no luck.
I know I can rewrite the URL with just the id like:
RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1/
Is there a way in PHP to overwrite the URL displayed?
At the moment you're wondering how to convert your ugly URL (e.g. /view.php?mode=prod&id=1234) into a pretty URL (e.g. /products/product-title). Start looking at this the other way around.
What you want is someone typing /products/product-title to actually take them to the page that can be accessed by /view.php?mode=prod&id=1234.
i.e. your rule could be as follows:
RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1
Then in view.php do a lookup based on the title to find the id. Then carry on as normal.
One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and your php file will have a script like this one:
// get the url
$uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
$query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
$url = str_replace($query,'',$uri); // you can edit this part to do something with the query
$arr = explode('/',$url);
array_shift($arr);
// get the correct page to display
$controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/
$action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
}
of course you will have to work this code to fashion your application
I hope this helps
One way would be to output a Location: header to force a redirect to the chosen URL.

.htaccess add hidden php get variable for language selection

I have a multiple language website, and I use a php get variable to set the cookie for the language setting. I have multiple subfolders (http://www.site.com/es and http://www.site.com/de) that each have a respective .htaccess file. When accessing these folders, the .htaccess file does this to "silently" redirect the user and add the appropriate php variable:
-------
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
rewriterule ^http://www.site.com/es/$ http://www.site.com/?l=es [P,R=301]
rewriterule ^(.*)$ http://www.site.com/$1?l=es [P,R=301]
-------
When someone accesses the root directory: http://www.site.com, I want to add a ?l=en suffix "silently" to the url. How do I do that? Thanks.
I don't think it's possible in the method you want.
However, you could (in a round about way) modify your code:
RewritRule ... proxy_add_l.php
In which, the code would:
<?php
$_GET['l'] = $_REQUEST['l'] = 'en';
require 'index.php';
?>
Note this is really, really, bad, and should only be used as a last resort really.
Can't you just redirect them to /en which looks cleaner?
I think if you want a fixed value this is perfect possible with something like this:
rewriterule ^http://www.site.com/$ http://www.site.com/?l=en ...
I cant test the result cause am not at home now.

what is the best way to do that name?get=

ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']

Categories