How to generate php files on server? - php

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.

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

Treat directory as request for a PHP file

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.

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 remove all .php extension from a dir and all of its subdirectories?

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.

PHP: Best solution for links breaking in a mod_rewrite app

I'm using mod rewrite to redirect all requests targeting non-existent files/directories to index.php?url=*
This is surely the most common thing you do with mod_rewrite yet I have a problem:
Naturally, if the page url is "mydomain.com/blog/view/1", the browser will look for images, stylesheets and relative links in the "virtual" directory "mydomain.com/blog/view/".
Problem 1:
Is using the base tag the best solution? I see that none of the PHP frameworks out there use the base tag, though.
I'm currently having a regex replace all the relative links to point to the right path before output. Is that "okay"?
Problem 2:
It is possible that the server doesn't support mod_rewrite. However, all public files like images, stylesheets and the requests collector index.php are located in the directory /myapp/public. Normally mod_rewrite points all request to /public so it seems as if public was actually the root directory too all users.
However if there is no mod_rewrite, I then have to point the users to /public from the root directory with a header() call. That means, however that all links are broken again because suddenly all images, etc. have to be called via /public/myimage.jpg
Additional info: When there is no mod_rewrite the above request would look like this: mydomain.com/public/index.php/blog/view/1
What would be the best solutions for both problems?
Edit/Additional question:
Is there a way to make /public/ the base dir using plain htaccess code?
Write the app in such a way that it doesn't need mod_rewrite to function (at the cost of having "ugly" urls). Progressively enhance it with mod_rewrite to achieve the desired result. This probably means that you'll need to store some base path config info in your app.
I don't understand these problems at all.
Yes, this is surely the most common thing you do with mod_rewrite, yet with 2 conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
So, nothing hurt your existing images.
Why not to use just absolute path, e.g. /myapp/public/myimage.jpg, so, no virtual directory will hurt image path?
what about path info? You could use it without mod_rewrite
/index.php/path/to/another/file.jpg
<?php
echo $_SERVER["PATH_INFO"]; // outputs /path/to/another/file.jpg
?>
Anyways, if you want to know if mod_rewrite is supported by your server :
<?php
echo "mod_rewrite : ".(!empty($_SERVER["REDIRECT_URL"])?"supported":"not supported");
?>
Then you ll know if mod_rewrite is the solution or maybe path_info is more well suited for you, you could make support functions that could look for both too.

Categories