This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.
Related
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/
I have a flash game site, which has a play.php file, which gets the game's name by $_GET like
http://host/play.php?game=free-kick-puzzle
which works nicely but I think it's not so good for google bot etc. And I see that many other sites do it like
host/games/free-kick-puzzle.php
So is there any way to "generate" this gamename.php file automatically?
Thanks !
You don't want to "generate" files on the server, you just want to create nicer URLs that map to the ugly ones behind the scenes. One way to do that is by using rewrite rules.
Using an Apache module called mod_rewrite, you can make URLs more SEO friendly with directives you write in your .htaccess file.
In your .htaccess file, you could use code like this:
RewriteEngine on
RewriteRule ^games/(.*)/?$ /play.php?game=$1
This is just an example, and there are many other options available when using rewrite rules. I highly recommend looking at the documentation in the Apache docs about mod_rewrite.
In this example, you're finding anything after games/, represented by the parenthesis and the RegEx in between them. The /? means the trailing slash is optional. After the $ is what you actually want to serve to the visitor. The $1 is where whatever is found in between the parenthesis is placed.
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.
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.. ...
My web is dir structured is as follows :
moving/
includes/
controllers/
includes/
etc...
I have the following questions about /moving and all of its sub-diretories:
How can i request any php script without the .php extension?
How can i request ...dir/index.php?q=about as just ...dir/about?
Where woul i place my .htaccess file?
I understand that my first question might be a duplicate, but in order to contextualize my second and third question i had to ask it.
Using WampServer 2.0
Thanks in advance.
apache will not parse your php script if they don't have the ".php" extention.
but, you can use mod_rewrite:
This module uses a rule-based
rewriting engine (based on a
regular-expression parser) to rewrite
requested URLs on the fly. It supports
an unlimited number of rules and an
unlimited number of attached rule
conditions for each rule, to provide a
really flexible and powerful URL
manipulation mechanism. The URL
manipulations can depend on various
tests, of server variables,
environment variables, HTTP headers,
or time stamps. Even external database
lookups in various formats can be used
to achieve highly granular URL
matching.
This module operates on the full URLs
(including the path-info part) both in
per-server context (httpd.conf) and
per-directory context (.htaccess) and
can generate query-string parts on
result. The rewritten result can lead
to internal sub-processing, external
request redirection or even to an
internal proxy throughput.internal proxy throughput.
If all your website files are ending with .php and you want to take off the .php extension, then you should create and .htaccess file, or if you already have it, add this rule:
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
Supposing your website pages are let's say site/about.php, site/contact.php, site/news.php, the rule above will just transform those to site/about, site/contact, site/news
Hope it helps!
Provided you have mod_rewrite routing non-php extension request to the proper request, you would put it in your root web directory.