Possible to remove extentions on php pages? - php

When I go to some websites I notice that there is no file extension on the page. Actually, this site is a proper example. Anybody know how this is done? :]

This is generally accomplished with URL Rewriting (link goes to a great introductory tutorial).
In the simplest case, a rewrite rule simply redirects every address by adding ".php" (or whatever other file extension) on the end of the url. So if someone tries to visit http://www.yoursite.com/about-me, they are actually viewing the content coming from the file about-me.php.

This could also be an MVC framework. (Search here, there are lots for php, a famous one for .NET, and I am sure many frameworks for many language.
This site has a root file, lets call it index.aspx (its not, but go with me) and that file has special code to read more of the URL. Used in combination with URL rewriting as mentioned, that special file takes in what would normally be path information and treats it as variable input.
Possible to remove extentions on php pages?
is something like
https://stackoverflow.com/index.aspx/questions/778799/possible-to-remove-extentions-on-php-pages
With index.aspx being the file that is hidden via URL rewriting.. Questions is a parameter indicating the controller, 778799 being a parameter that and the other text being the final parameter. (That is ignored)
For more see
http://www.asp.net/mvc/
http://codeigniter.com/
http://en.wikipedia.org/wiki/Model-view-controller

Aside from URL rewriting, you can also change apache's configuration to support alternative file extensions allowing you to do do something like MyPHPPage.html.
Regards,
Frank

Use mod_rewrite. Just put code like this in a file called .htaccess on your root:
RewriteEngine on
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?resource_type=$1&id=$2 [QSA,L]
Notice how $1 and $2 access the two quantities in parenthesized portions of the regular expression. It seems easy to extend the code from here

Related

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.

Is it possible to REWRITE a URL without htaccess file?

Is it possible to shorten a url without using htaccess file?
for example I have this url.
this/is/a/very/long/url.php
change to
short/url.php
I hope I can get good answer THX guys:)
yes, mostly used in frameworks
an approach called using a front controller
ex: your front controller is index.php
your page links are generated as a fashion of .../index.php/nything/url.php
but the actual link is .../this/is/long/url.php
the front controller extract the page information the client requested and show the relevant page related to it
read more : http://en.wikipedia.org/wiki/Front_Controller_pattern
This is a good question, there are multiple options however the .htaccess file is probably your best bet.
this SO post describes it
Handling the url rewriting serverside is key here since it will be much faster to execute and will not break your script when used on some URL's.
so www.yourdomain.com/test/4/twenty/long/url/could/be/shorter/
all the arguments after www.yourdomain.com, can be retrieved via various PHP methods, including reading up on PHP's $_SERVER would be a good idea, as lots of variables are placed in that global array.
mod_rewrite is part of Apache, so it needs to be configured in Apache config. .htaccess actually Apache "live" config "per directory".
In general the best way ( I think) to rewrite urls is to rewrite everything to one file, let's say index.php and then "redirect" to specific file basing on URL. You can read a lot about "URL routing" in PHP on the web.
You can use this code in Javascript for rewriting current URL:
if (location.href.indexOf("this/is/a/very/long/url.php") > -1)
location.assign(location.href.replace(/this\/is\/a\/very\/long\/(url\.php)/, "short/$1"));

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

php based website that displays page content based on URL?

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

Is wordpress SEO friendly url works without using htaccess?

Wordpress uses friendly seo url without using htaccess.
Can any explain this to me please how they do it.
The only way i can think of is to do something like this.
domain.com/index.php/nnn/mmmm/
But wordpress does not use index.php
I know they are not using htaccess.
Please let me know.
Thank you.
WordPress actually has a single .htaccess file that they don't need to change which redirects all requests to index.php
index.php then looks at the permalinks rules and runs a few database queries to determine which page to send you.
So for instance if the permalink rule is %postdate%/%postname% (may not be the actual WordPress permalink variables. I haven't been using WordPress for too long) then it would just use regular expressions (or combinations of substr() and strpos()) to put %postdate% and %postname% into variables. Next it runs a simple database query for any item matching that date and that name. If nothing is found, redirect to search. If you find more than one, list them all (like a category page). If you find one and only one, send that page.
As far as actually "sending" the page, that's just a matter of settings certain variables (such as $the_post['content']) and then include()'ing the proper theme file.
include()'ing the theme file is a simple if() statement.
if(file_exists("wp-content/themes/<your_theme>/$the_post['type'].php")){
include("wp-content/themes/<your_theme>/$the_post['type'].php");
} else {
include("wp-content/themes/<your_theme>/index.php");
}
Mind you these aren't the exact variable names or the exact functions as they occur. This is just a very simplified version to give the general idea of how these systems work.
Wordpress does use .htaccess files. You can modify the permalinks in the permalinks section your wordpress site. You can see information on it here.
If you are using IIS you can use this method
Wordpress does actually use .htaccess. Make sure you have hidden files displayed! If you're FTP-ing, there'll be a setting in your client.
To answer the main question:
No.
As for the other points of yours:
Wordpress uses friendly seo url without using htaccess.
USUALLY it does not.
USUALLY it requires the .htaccess to make nice urls work.
However, as Wordpress' Codex says:
If you are using IIS 7 and have admin rights on your server, you can
use Microsoft's URL Rewrite Module instead. Though not
completely compatible with mod_rewrite, it does support WordPress's
pretty permalinks.
So they are always using some kind of rewrite module anyway.
Can any explain this to me please how they do it.
There is one interesting thing to explain:
There is only one .htaccess file.
Which would not be so interesting if it would contain some kind of general rule rewriting urls to some.php's GET parameters (domain.com/verynice => domain.com/index.php?page=verynice).
But that is not happening.
What is happening is that
It rewrites ANY url that is NOT a specific FILE or DIRECTORY to index.php EXCEPT the index.php (to prevent infinite loop).
So urls like domain.com/blah/blah and domain.com/lol/trololo are both actually requesting the index.php - the only difference is in the requested URI.
Using superglobal variables like $_SERVER['REQUEST_URI'] they get the requested URI and after that they parse the requested URI and store the results in varibles accordingly.
The only way i can think of is to do something like this.
domain.com/index.php/nnn/mmmm/
In common explanation of the URL this is NOT request to the index.php file itself.
It is a request for the "mmmm" folder in folder "nnn" which is in folder "index.php".
However apache usually understand this as a request for the index.php file .
For other files e.g. my.file domain.com/my.file is a file request, domain.com/my.file/ is a folder request (notice the slash / at the end)
But wordpress does not use index.php
That is NOT TRUE.
Especially if you are using nice-urls the index.php is practicaly fundamental for Wordpress.
I know they are not using htaccess.
This point of yours could meant 3 things:
They are using the htaccess but not for processing the URL (which I've explained above)
They are not using it at all - not true, unless they would be using the MS' URL Rewrite Module
You don't know. You did not provide any information about how did you find out they are not using it, so we can't know if you really know or you just think that :)
Please let me know.
I let you know. And I hope I've answered all your questions :), if not - use comments :)
Thank you.
You are welcome.
I was happy to help :) ☺ ☺ ☺ ☺ ☺

Categories