php based website that displays page content based on URL? - php

I would like to make a website that is php and only requires one php document to display the content. It's kinda hard to explain, so let me try my best.
Some websites use a directory based system such as this:
web.URL.com/index.htm
web.URL.com/about.htm
web.URL.com/blog.htm
and so on.
This involves creating a text file for each page.
So, the goal here is to create one page that acts like a frame, and displays content based on the Url, so it will be acting like the above method, but is only one page. I'm pretty sure you can do this, but don't know the proper way to word it or what vocabulary to use when typing it in to google.
Some questions you may have:
Q:Why not use parameters like this:
web.url.com?pgid=some+page
A: I would like the url to be as clean as possible when it comes to the visitor typing the url. For example, this is much easier to remember:
web.url.com/about
than this:
web.url.com?p=about
Q: Wordpress??
A: We want our own stuff. Wordpress is great, but not for our project.
Q: JQuery?
A: PHP please.
Thank you in advance!

With PHP alone, I don't think it's possible. However, it's likely that your webserver is running Apache, which makes this task fairly straightforward by transforming your pretty URLs into ugly URLs (with query strings) that your PHP script can handle. Your visitors have no idea this is happening; they aren't redirected or anything.
I strongly recommend you take a look at this tutorial, and its followup. The second tutorial contains the information you need for this task (starting in this section), but I strongly recommend reading the first tutorial to gain a deeper understanding of what you're doing.
Essentially, those tutorials will teach you how to specify (ugly, scary-looking) URL-rewriting rules in a file called .htaccess. These rules will transform (or "rewrite") your pretty URLs into less pretty ones that your PHP script can handle.
For example, yoursite.com/about would actually be accessing yoursite.com/index.php?page=about, or something along those lines.
For some sample code, here's a snippet 95% copied and pasted from this tutorial:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
As that link explains, it will forward all requests to index.php, unless an actual file or directory is being requested.

Use a RewriteRule based .htaccess. Then you can have all non-existing files redirected (internally in Apache) to a general index.php with the requested path as a query string ?p=about. The redirecting is handled internally, so http://example.com/about is the external URL to that page.

You can have a switch statement checking $_GET values and output the page based on the provided parameter. Also, you will have to add some mod_rewrite rules to route those requests.

Use PHP URL rewrite. PHP will enteprate part of your URL as query string

Related

GET request without needing the ? at the end

I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?
For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones
Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.
But how can I change that link above to:
http://www.example.com/user/JimmyJones
Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?
EDIT:
Here are some websites that do it just about how I would like to do it:
imgur.com/user/example
facebook.com/exampleuser
you make .htaccess file in the root dictionary then start it with
RewriteEngine on
then write your rules, For your example it would be like this
RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]
so a full page would be like this
RewriteEngine on
RewriteRule ^/?user/([^/]+)/?$ user.php?user=$1
just for your example.
In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:
RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]
The first line says use the rewrite engine.
The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).
The L flag basically says job done.
If the .htaccess were in the public root:
//example.com/user/JimmieJones
would map to:
//example.com/user.php?user=JimmieJones
However it will not match:
//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9
Note that any existing links in your application:
Visit Jimmie's Profile
Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.
Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.
I reserve first level patterns for aliases/shortcuts/campaigns.
//example.com/slug
I'd avoid that for your user profile urls if possible.
You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).

Create a rewrite rule in .htaccess for php file

I am very new to .htaccess, I am having some problem with file action.
Example:
http://www.domain.com/upload_pic.php?action=save_pic
I wrote the .htaccess rule like
RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!##$-_]*)$ upload_pic.php?action=$1
I want the desired result like:
http://www.domain.com/sabc/save_pic
How can I get the desired result, please correct my .htaccess line.
Change it to:
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
The .+ will capture one or more characters after the / and be captured into $1
There are several other guides on the web already, but to understand it in better way as you are beginner #AMY, I have writen this for you. Hope this will work for you.
Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.
http://www.domain.com/upload_pic.php?pic_id=7
The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:
http://www.domain.com/sabc/7/
Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:
http://www.domain.com/sabc/user-navnish/
Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.
Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^sabc/?$ upload_pic.php [NC,L] # Handle requests for "sabc"
A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single
RewriteRule.
^/sabc/?$ - The "pattern". The server will check the URL of every
request to the site to see if this pattern matches. If it does, then
Apache will swap the URL of the request for the "substitution"
section that follows.
upload_pic.php - The "substitution". If the pattern
above matches the request, Apache uses this URL instead of the
requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this
case, we're using two flags. "NC", tells Apache that this rule should
be case-insensitive, and "L" tells Apache not to process any more
rules if this one is used.
# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended)
The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
Hope this will be helpful for you to understand URL rewriting #AMY.

best way to url rewrite a large website

i have a large database and i would like to url rewrite. it is like
www.example.com/products.php?id=111&cat=99&f=6&hjk=4545..
So I wrote url rewrite for this code to appear as
www.example.com/men/watches/casio/latest.. like that and its works fine
The problem is that I have a large database and its nearly impossible to write all url rewrites separately.
I thought of generating .htaacess file automatically using php. Does that make sense? or whats the best solution for this..?
EDIT: For #ghoti
RewriteEngine On
RewriteRule ^/([^/]*)/check/([^/]*)/SubjectId/([^/]*)/UniversityId/([^/]*)/CourseId/([^/]*)\.html$ /dsh_course_subjects_topics.php?TopicId=$1&check=$2&SubjectId=$3&UniversityId=$4&CourseId=$5 [L]
You need a single rewrite rule to catch the pattern you are looking for and pass it to a php page. For example:
(note, all of this needs to be on the same line)
RewriteRule
^/(men|women)/([a-z]+)/([a-z-])/(latest|newest|expensive|cheapest)$
/products.php?gender=$1&product=$2&brand=$3&filter=$4
In that way, you can pass generic request on to PHP where you look at $_GET to find out what the dynamic values were, and then pull up the correct items.
If you look at StackOverflow, their URLs look like this:
^/questions/([0-9]+)/([a-zA-Z0-9-]+)$
Spend some time looking at ModRewrite documentation as well as Regular Expressions. Then think how you can positively identify your product (eg, you may need to incorporate an ID in the URL nonetheless).

Dynamic Website while still being SEO compatible [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htacces to create friendly URLs. Help needed
I would like to have create a website, which loads up content from a MySQL database via PHP, while still being optimized for search engines.
I have considered a few approaches and was looking for some advice on which way would the best.
Users on my website would fill out a form and submit it, which gets saved in the SQL database. I would like to have it so that each new page could be accessed like this:
domain.com/plugins/whatever
domain.com/plugins/anotherpagename
Some approaches:
1.
A script generates a static HTML page in the plugins folder when the form is submitted. When the page is edited (there will be a edit button), the HTML page gets overwritten with the updated information.
domain.com/plugins/whatever and domain.com/plugins/anotherpagename would all redirect to a PHP script.
However, I do not know how to create a .htaccess which would allow the PHP script to get the page name so that it can search the SQL database and bring up the correct information.
I am also unsure about how search engines would treat these pages?
Maybe someone else can come up with an even better approach for this?
Thank you in advance for your help.
You have a few options for how to do this. The simplest way that matches your requirements would be to add the following to your .htaccess file:
RewriteEngine on
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L]
The above pass all requests to /plugins/anything to yourscript.php, from which you can access the plugin name using $_GET['plugin'].
If "plugins" is simply an example, and you want to be able to use /anything/anything - then you may wish to look at passing all requests to your PHP script, and then parsing the requested URL.
To do that, you'd use something like this in your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /yourscript.php [L]
To break that down, the above tells Apache to rewrite all requests where the file does not exist (i.e. images/css/js, etc.) to yourscript.php.
From there, you can access the whole URL via $_SERVER['REQUEST_URI']. Which you can break down with string functions, regular expressions, and so on.
why not make an actual directory ?
Eg.
domain.com/plugins/whatever
domain.com/plugins/anotherpagename
whatever and anotherpagename would both be directories.
Then write the html file as index.html inside the directory.
No htaccess needed and you could still access using the same url as above.
I don't know if you are using any framework. I have solution using codeigniter. The same can be simulated using other frameworks / core PHP.
codeigniter specific solution:
In config/routes.php define
$route['plugins/(:any)'] = "plugins/getPageFromDb/$1";
After this, in browser the url will be same 'domain.com/plugins/whatever' but actually control will hit 'getPageFromDb' function of the 'plugins' controller with 'whatever' as the parameter.
In 'getPageFromDb' function you should write your code to get the page from db based on the parameter 'whatever' and echo the output directly or through a view template (depending on your logic / scenario).
And for performance you may keep a static version of the html page and write a relative check in the 'getPageFromDb' function to decide whether to get the page from static or from db.
Considering the url format and the well formed HTML rendering, it should be Search Engine friendly page.

Can I re-write my URLs like this and is it a good idea?

Is it possible to re-write (Apache Mod-Rewrite) a URL from this:
http://www.example.com/view.php?t=h5k6 to this http://www.example.com/h5k6
The reason for this re-write is that the URL needs to be very short (a bit like a tiny URL service).
Would that new URL still hit my view.php page? Would it be able to still make use of the super global array GET ($_GET) to access the variable t? I still want my index.php page to map to this http://www.example.com.
I would also appreciate comments on effects this may have as I am a bit of a noob. :)
Thanks all
The terminology of mod-rewrite works the other way. Requests would come in like this http://www.example.com/h5k6 and would be rewritten to http://www.example.com/view.php?t=h5k6 internally. That way your PHP scripts can respond to the requests as if they were sent as GET parameters, but users see the URLs in a much friendlier way.
So you don't have to change any PHP scripts, and you can still access the parameter t from the GET array.
In your case, the rule would look like this:
RewriteRule ^/(.*) /view.php?t=$1
You might want to be more specific about what you accept (instead of just the catch-all .* expression). Also, you might want exceptions to this rule if you have other pages in this directory other than view.php.
Try one of these:
# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^/view\.php$ /%2 [L,R=301]
# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^/([0-9a-z]+)$ view.php?t=$1 [L]
Yes, this is definitely possible and will have exactly the effect you describe. However, your terminology is backwards. The "short" URL is the one the browser originall sends to the server, and it is rewritten to the longer version that is then actually processed and results in a PHP request.
Pretty much the only catch is that if used carelessly, this could result in search engines indexing both URLs and considering them duplicate content, which is bad for your ranking. On one hand, this can (and should) be avoided by never publishing the "long" version anywhere (i.e. all site-internal links have the "short" format). However, since you might publish them accidentally (which can happen easily), you should also use link rel="canonical" to tell search engines the URL you want your pages indexed under.

Categories