Clean URLs and Getting Section Name - php

I'm cleaning up my URLs and running into an issue with getting the section/sports name (e.g. football) to use to determine which section of the navigation to highlight/display.
Example URL:
http://example.com/index.php?sports=football
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?sports=$1 [NC,L]
So far, so good. Then I attempt to grab the section name, but it's not working:
$url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $output); // $output['sports'];
Is this because I need to grab the URL that is visible in the address bar?
http://example.com/football/college
If so, can someone explain the order that things are processed (e.g. .htaccess, php, etc.)?

You can just read the query parameters through the $_GET global array.
So in your case you can just access it as follows:
echo $_GET['sports'];
Regarding the order of accessing files. First of all Apache will receive a request to a resource in a public directory, the directory contains .htaccess, this file contains instructions for Apache to handle the request for files in that directory.
After that, Apache will find the file is PHP file so it hand it over to PHP to process, the output of the script is included in the Response served by Apache.

Related

PHP Fundamental Configuration of a CMS

My question is very fundamental: The basic idea of a CMS is that there aren't real content files but in the simplest scenario one single file index.php, which:
reads the URL like domain.com/fruit/pineapple(.php) or
domain.com?cat=fruit&sort=pineapple,
fills itself with the pineapple-content from a datasource,
will be send back then to the client with the alias of the request URL.
About 1) How does the server know that index.php is in charge for every request? Is it only htaccess? Wordpress:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Is this everything? Every nonexisting file is interpreted as an existing article of the content? So I have to check inside index.php that a forgotten image pineapple.png is sorted out?
About 2) How does the server rewrite the name index.php into /fruit/pineapple(.php) or ?cat=fruit&sort=pineapple ? This can't be a 301-rewrite, the server has to rebaptize index.php into the requested URL.
Question 1: Yes, RewriteEngine takes care of routing every call to index.php.
Edit: Actually, not every call, the lines with !-f !-d test if the call refers to an existing resource. It only routes to index.php if they don't exist. This allows for the server to send existing files (such as images and other included files, like js and css files) without help from index.php.
So, if I request domain.com/fruit/pineapple.jpg, and that image exists, I will get it. if it doesn´t exist, index.php gets called and it may generate a nice looking 404 page.
Question 2: No, this is not taken care by APACHE, it is taken care by index.php itself, by inspecting $_SERVER['REQUEST_URI'], and mathing its contents against a set of predefined routes.
This is called URL Routing. Each CMS has its own way of doing this, and there are also some libraries for PHP (or any other server-side language you prefer).
You can take a look at a longer explanation about How to Implement URL Routing in PHP

Rewrite PNG URL to PHP File

Please forgive me, but I'm VERY new to PHP, and even worse with url rewrites.
I'd like to write a PHP script that will dynamically output an image that will then be used as a signature on a forum that I belong to.
I have a base PHP file that I'm working with and will be editing that so I'm able to host it for other users of this forum.
The information will be stored in a database, and I'd like to call the PHP script with a PNG URL
Example:
http://somedomain.com/somecode.png
rewriting that to
http://somedomain.com/sig_img.php?img=somecode
Where somecode is the database table primary index.
I don't really need help with the PHP script (yet), but I have no clue where to begin with the .htaccess mod_rewrite code.
Thank you all for any assistance!
Assuming that mod_rewrite is enabled, something like this should work:
<Location />
RewriteEngine On
# ensure requested resource is not a file.
RewriteCond %{SCRIPT_FILENAME} !-f
# ensure requested resource is not a directory.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*\.png)$ sig_img.php?img=$1 [NC,L]
</Location>
This will redirect any request that is a not a file or directory to the script sig_img.php with the requested filename as the parameter. You may want to restrict this to only .PNG file requests, in which case please read about the options.
Make sure you treat the request as untrusted and parameterise the input; don't just concatenate $_GET['img'] into your query string.

Rewrite URL using PHP with a generated key

I want to rewrite my URL with an generated key.
So...
My base URL -> www.example.com
My problematic URL -> www.example.com/actions.php
And the final URL I want to be like www.example.com/41f1341df14r12
The problem is that i want to generate each page based on a key (41f1341df14r12) from my database (MYSQL).
P.S. I want to use PHP for that.
Thanks!
UPDATE:
Sorry for confusing. My English is not so well.
So... what i want:
In index.php I have a <input type="text">. When the user press the submit button my PHP generates a key (41f1341df14r12) and store both (key and input text) in database. I want when user press the submit to go to a page with URL like
www.example.com/41f1341df14r12
and on that page to see his message.
I hope to understand now :D
First your apache need mod_rewrite Url. Then you can use this code in your .htaccess to redirect the key to a php file that can process the key.
RewriteEngine On
# Rewrite only if its not a file
RewriteCond %{REQUEST_FILENAME} !-f
# and if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request to action.php
RewriteRule .* action.php [L]
In the PHP Script you can read from the super global variable $_SERVER['REQUEST_URI'].
<?php
echo substr($_SERVER['REQUEST_URI'], 1); //remove the first /
Alternative you could redirect the key to a GET variable:
RewriteRule (.*) action.php?key=$1 [L]
One way to accomplish this is using URL rewriting. This is when the web server opens a certain (PHP) file if an address matches one of the set rules.
The process is different for every web server, I'm assuming you are using apache.
You will need to create an .htaccess file in the root of your web server and add the following lines:
RewriteEngine On
RewriteRule ^([a-z0-9]+)/?$ handler.php?key=$1 [L]
The first line enables URL rewriting. The second creates a rule that says: "If the requested URL matches the format 'example.com/key/', open the file 'handler.php' and pass it the key."
In your PHP file (handler.php in this example), you can access your key with $_GET['key'] and use it to generate the page.
For more information see this tutorial.
Notes:
You may need to restart the web server after creating the .htaccess
file.
This will not match uppercase letters, use ^([A-Za-z0-9]+)/?$ instead.
If you are not using shared hosting, you may need to enable mod_rewrite in the apache httpd.conf (see tutorial).

Path formatting for underscore or non-alphanumerics in URL for YII

I have paths like follwing
http://locahost.com/wayinfra/site/wayinfracms?view=about_us
http://locahost.com/wayinfra/site/project?view=justa_hotels
I want to use the urls as
http://locahost.com/wayinfra/about_us
http://locahost.com/wayinfra/project/justa_hotels
Added requirements - When i am using in url manager 'project/<view:\w+>'=>'site/project/' the url locahost.com/wayinfra/project/justa display perfect but locahost.com/wayinfra/project/justa_hotels generate error unable to find the request.
how i can do this?
Read the following document:
Yii's URL Management
Source - http://www.yiiframework.com/doc/guide/1.1/en/topics.url
You first need to configure the Web server so that a URL without the entry script can still be handled by the entry script. For Apache HTTP server, this can be done by turning on the URL rewriting engine and specifying some rewriting rules. We can create the file /wwwroot/blog/.htaccess with the following content. Note that the same content can also be put in the Apache configuration file within the Directory element for /wwwroot/blog.
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Then configure the 'showScriptName'=>false in the urlManager array.
Use code below for alphanumerics and underscore in the parameter as required.
`'project/<view:[a-zA-Z0-9_]+>'=>'site/project/'`

Redirect based on url arguments (via PHP or cPanel)

I just started using Amazon S3 to host static files (images, videos, etc.).
For accessing the uploaded files, temporary links are created.
A temporary link looks like this one:
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=1346888760&Signature=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
What I want is to serve these file through my url, something like this:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
I know I can redirect requests to http://s3.mydomain.com to the Amazon url via PHP (for example), but I don't want the address bar to change.
I can create a .htaccess to transform the url to the Amazon url, but as I know .htaccess can't redirect to external resources.
So, how can I solve this?
There are a couple of solutions:
.htaccess Solution #1 - Rewrite Rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^s3\. # Hostname starts with "s3."
RewriteCond %{REQUEST_URI} !-f # Not a file
RewriteCond %{REQUEST_URI} !-d # Not a directory
RewriteRule ^/(.+)/(.+)/(.+)/(.+)/(.+)$ http://$1.s3.amazonaws.com/$2?AWSAccessKeyId=$3&Expires=$5&Signature=$4 [R=302,L]
NOTE: Your initial desired URL was missing the "Expires" value, so the above would work for URLs formed like so:
http://s3.yourdomain.com/[[S3_Bucket_Name]]/[[S3_Filename]]/[[AWSAccessKeyId]]/[[Signature]]/[[Expires]]
So:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D/1346888760
would redirect to
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
.htaccess Solution #2 - Redirect
Whilst being a less flexible solution than the above, you could put the following into your .htaccess file
redirect 302 /s3/ http://zeebit.s3.mydomain.com/
Using this rule, requests for
http://yourdomain.com/s3/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
Would basically retain everything after /s3/ and simply replace everything preceeding it with the Amazon S3 location.
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760

Categories