Clean url in php using htaccess - php

Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help

This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.
Here is the .htaccess code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4

Related

Short URL with htaccess on many subfolders with same file

I have this situation:
There are many scripts that can be accessed like this:
http://website.com/script1/
http://website.com/script2/
Each script has a display.php file that receives a variable from the index.php file VIA a link. I'm using the $_GET method to reat that variable.
Example:
http://website.com/script1/display.php?id=124
What I'm trying to do is to access the display page like this:
http://website.com/script1/124
http://website.com/script2/124
Does anyone have any suggestions? My experience with htaccess is ~0.
RewriteRule script[\d]+\/[\d]+ /script$1/display.php?id=$2
This would rewrite anything like script345987/22352 to script345987/display.php?id=22352
Edit if you want to match anything, not only script, then:
RewriteRule (.*)\/[\d]+ /$1/display.php?id=$2

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.

URL rewrite with PHP & MySQL and htaccess

I have my site as localhost/tutorials/rewrite/news.php on this page are titles from news articles that are in my database. Each title is a link to read the article. The link is
'' . $title1. ''
The read-news.php page gets the url and uses it in an sql statement to get the article from the database
$url = $_GET['url'];
$sql = "SELECT * FROM news WHERE url = '$url'";
My link on the news.php page and the url on the read-news.php looks like this
localhost/tutorials/rewrite/read-news.php?url=the-news-today
How can i get it to look like
localhost/tutorials/rewrite/read-news/the-news-today.php
I have used the following htaccess code which by looking at other examples i thought should be enough to fix it
RewriteRule ^read-news/(.*).php /read-news.php?url=$1 [PT]
Any help please
edit: your RewriteRule needs some work too. try this:
RewriteRule ^read-news/(.*)\.php$ /read-news.php?url=$1 [PT]
(you need to use a "$" to signal the end of your rewrite and escape the . before "php" with a backslash)
Also, you should link to your pages how you want them displayed:
href="localhost/tutorials/rewrite/read-news/the-news-today.php"
The .htaccess won't magically change the urls for you.
Also if you want to be able to access your query string variables, i would add QSA to your flags: [PT,QSA]
that way you can still access your url variable with $_GET['url'].
Do it how Wordpress does it. Rewrite everything into index.php and parse the url from script.

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.

How To rewrite a url with 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

Categories