How to make a virtual Directory? - php

http://wiki.answers.com/Q/What_do_the_abbreviations_mean_on_an_ultrasound_scan
In above url, it looks like "What_do_the_abbreviations_mean_on_an_ultrasound_scan" is a directory!! but i think its a virtual directory !!
I want to know about this stuff !! how to do these things and what they call ???

They are most probably using mod_rewrite to rewrite the url. A good resource is the tag wiki for mod_rewrite here on StackOverflow.
The rules for mod_rewrite is stored in the .htaccess file, and looks something like this:
RewriteRule ^/Q/(.*?)$ answers.php?slug=$1
This would rewrite all requests whitch match a url starting with /Q/ to answers.php, and provide whatever is after (in this case What_do_the_abbreviations_mean_on_an_ultrasound_scan) as a GET parameter. This would be accessible in the script as $_GET['slug'] if they were running PHP.

Related

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

CakePHP routing without trailing path info

I have a simple CakePHP which I want to use without URL rewriting. The site which works fine locally but not on my hosting server. My sysadmin has advised me that:
The PHP wrapper doesn't accept trailing path-info like script.php/additional/path/info
CakePHP seems to expect this by default. How do I get around this limitation? I guess I need to pass the path information via the query string instead, so instead of URLs like this:
http://example.com/index.php/controller/action
I probably will need something like this:
http://example.com/index.php?path=controller/action
I'm sure this should be doable by changing the routing scheme somehow, but how? I have tried reading the docs, and digging into the framework source code is kind of daunting... Thanks!
Your simple solution is how to remove trailing slash from URL using .htaccess. This may be done by the following rewrite rule:
RewriteRule ^(.*)/$ $1 [R=301,L]
This should be placed in the .htaccess file where your application's index.php.

How to manage URLs in PHP

I have urls like:
example.com/category?id=5,
but I want this url look like
example.com/category/category_name.
I use pure php without any framework.
Can you please suggest me any way to do it..?
This is something you should try to solve with your webserver, it's not really an PHP question. For Apache, the mechanism is called modRewrite.
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html should be an easy starter.
If under Apache, use some .htacces mechanism.
Anyway your beautified url should contain some reliable data as the category id, not only the category (sluggified) name.
Let's say you want an url like :
example.com/category/category_name/id
In your .htaccess you'll use a rule like :
RewriteRule ^example.com/category/([a-z0-9\-]+)/(\d+)$ /example.com/category?id=$1 [L,R=301]
Check your web sertver documentation for details explanations
look into using mod rewrite as part of apache. You will have a .htaccess file in the root of the web server (if not create 1 and then add the rules you need)
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
there is a site that you can provide a url from a page on your site and it will create the mod rewrite code for you
http://www.generateit.net/mod-rewrite/

Domain/URL Masking

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.. ...

How to translate /en/file.php to file.php?lang=en in htaccess Apache

I'm writing multilingual website. I have several files on server like:
/index.php
/files.php
/funny.php
And would like to add language support by placing language code into URL like this:
http://mywebsite/en/index.php
would redirect to:
http://mywebsite/index.php?lang=en
And
http://mywebsite/en/files.php
would redirect to:
http://mywebsite/files.php?lang=en
I would like to put more languages for example:
http://mywebsite/ch-ZH/index.php
And I would like this to work only for files with php and php5 extension. Rest of files should be the same as they are.
So for example when i will go to address
http://mywebsite/ch-ZH/index.php
I would like my PHP to recognize that current path is
http://mywebsite
and NOT
http://mywebsite/ch-ZH
It's necessary for me because in my PHP code I relate on current path and would like them to work as they are working now.
Could you please write how to prepare htaccess file on Apache to meet this criteria?
Try this:
RewriteEngine on
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
And for the current path problem, you have to know how relative URIs are resolved: Relative URIs are resolved by the client from a base URI that is the URI (not filesystem path!) of the current resource if not declared otherwise.
So if a document has the URI /en/foo/bar and the relative path ./baz in it, the client resolves this to /en/foo/baz (as obviously the client doesn’t know about the actual filesystem path).
For having ./baz resolved to /baz, you have to change the base URI which can be done with the HTML element BASE.
If you don't want to use ModRewrite, you can put symbolic links in the web folder (ln -s . en) and check the URL in PHP.
Something like this should do the trick,
RewriteEngine on
RewriteRule ^(.[^/]+)/(.*).php([5])?$ $2.php$3?lang=$1 [L]
This will only match .php and .php5 files, so the rest of your files will be unaffected.

Categories