Domain/URL Masking - php

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?

.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.

You should look into writing some apache Mod_Rewrite rules in a .htaccess file.

The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Related

How to create dynamic webpage with custom name?

I have looked around and attempted my own research on this topic but to no avail just yet.
I have a dynamic webpage set up to look for a ID from a database to retrieve elements required. This results in of course the web page looking like www.site.com/page?id=1
My desired outcome would be like a title for this page to be called.
Such as say I had a fruit product it and user went to my site and went to the address /fruit it would it would be the content of ?id=1 just as an example.
I have seen this used on many a site but not sure how this is programmed or works. Is this something to do with a htaccess document?
Thanks in advance. Appreciate all the help.
While this has been asked and answered many times, I know many people find it difficult to search for this since there are so many common "noise" words related to it. For that reason, I believe it's worth answering again.
If you're using Apache as your webserver (which I'm assuming you are since you mention .htaccess), what you're looking for to create those "clean URLs" is mod_rewrite, which takes a set of rules and rewrites the URL requested by the browser to another path or script.
You would typically enable this in your Apache config or in .htaccess, and in a simple form (a one-to-one mapping) at it would look something like this (provided mod_rewrite is installed):
RewriteEngine On
RewriteRule ^fruit$ index.php?type=1 [L]
Now obviously that doesn't scale well if you have a bunch of dynamic pages you want to create, so what you can do is tell all pages that aren't a really file or directory to be passed to a file for processing, like so:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
In this case we're rewriting any request that doesn't resolve to a real file or directory to index.php, and then using the "last" flag [L] to stop processing other rules. Then in our PHP script, we can access the virtual path (in this case /fruit) by using $_SERVER['PATH_INFO'] and doing whatever conditional logic we want with that. If you don't get anything in that variable, ensure that the AcceptPathInfo On directive is set in your Apache config or .htaccess.
A way to test the basic concept/logic without having any rewrite rules would be to use a URL like https://example.com/index.php/fruit. You'll then see that in index.php $_SERVER['PATH_INFO'] will contain the string /fruit. You can rewrite URLs to files in other directories, chain rewrite rules, redirect the browser to other URLs, or even edit environment variables.
There are many good tutorials around using mod_rewrite for clean URLs, so I won't attempt to cover all the nuances here. Just know that it's a very powerful tool, but it's also pretty easy to break your rules if you aren't very comfortable with regular expressions or get lost in the many rules that are commonly in a configuration.
Note that if this is an existing site, you'll also want to use mod_rewrite or mod_redirect to redirect the old URLs to the new ones so they don't break (and for the benefit of having a single URL for search rankings).

PHP GET request but no ?=

I am trying to find out how websites like Imgur, MEGA and such are able to do this:
https://imgur.com/a/SbmNz (emphasis on SbmNz)
The SbmNz bit is dynamic between images or files and I guess it is a kind of $_GET. And I was just wondering how you can do this without the usual ?name=value way.
you can use any MVC or just write a htaccess rule for it , for instance, you can use laravel to pass variables along with url
for laravel refer URL Generation-Laravel5
to write .htaccess refer USING .HTACCESS REWRITE RULES
You can do with Apache rewriting module by changing .htaccess (Create .htaccess file in your folder)
For example if you have this snippet
RewriteRule ^play/([^/]*)$ player.php?id=$1 [L]
When user visits www.example.com/play/Trg4 in backend request is actually handled for www.example.com/player.php?id=Trg4
Then you can get the id with $_GET['id']. it means there is no change with php code. it is completely done with .htaccess
Make sure you enabled rewrite_module of your Apache server

Mapping URL into custom files

I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.

hidequery string parameters from the url using url rewriting

In my php web site, I need to display my urn in a specific manner
I need to hide the query string parameters and just want to display the value in the URL
i.e.
mydomain.com/city.php?place='usa'&id=2
I need to display
my domain.com/city-2/usa
Is it possible?
Please help me. I am in a serious situation.
Thanks in advance
Yes, it is possible:
RewriteEngine On
RewriteBase /
RewriteRule ^city-(\d+)/(.*) city.php?place=$2&id=$1
You need to put it into the .htaccess placed in the web root of your site. To get it worked you need to have .htaccess parsing enabled and mod_rewrite apache module (assuming you have apache as a web server) enabled as well.
More in official documentation

Replace articles.php?id=123 with /articles/123-title-of-article

I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks
Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.
The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.

Categories