This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rewrite all queries to not need the .php extension using a mod_rewrite RewriteRule
I am learning how to create a website, and would like some help.
I have this path in the wamp folder: C:\wamp\www\Personal_site\Root_fold\index.php (the main file starts here).
I want to have cleanurls for this file: C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp.php, and I want it to show up as C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp - the same file, but without the .php on the end.
I also want this to happen to every other file that I have in my website - even the index.php - without repeating the rewritecond for each and every file. How do I do that?
By the way, I am running this locally on my computer - no hosting service or anything like that - and also I am creating the .htaccess file by opening it in notepad and saving the file with the name ".htacess", which means it forces the file to save with the .htacess extension.
Clean URLs go beyond just hiding the .php extension. It also needs to incorporate query parameters, so that instead of /article?title=foobar it looks like /article/foobar.
This problem was solved over and over and over again. Please don't participate in the PHP community's pervasive Not invented here syndrome and use one of the existing solutions available. It will make you a better programmer in general, because today's programming is about artfully combining existing components with your unique domain logic instead of writing everything from scratch.
UPDATE
Since you are just starting, it might be hard for you to start using a full-blown framework like Symfony2 — but I suggest to use it when you'll be more comfortable with PHP.
For now, the Silex micro-framework might be a good start for you. It's very easy to start with. As a simple example, here is your index.php file:
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
And this is your .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is a complete app written in Silex. When you go to <yourhost>/hello/JackyBoi it will respond with Hello JackyBoi.
You can do more advanced stuff with Silex — details are in the docs. When you outgrow it, you can naturally graduate to its bigger brother Symfony2.
Add this to your .htacceess:
Options +MultiViews
Remove all the rewriterules. Then you can use URLs (not filenames!!) without the .php extension.
Related
I am developing website using php and mysql.my each page file extension end with .php extension how do i avoid that?what I have to do for that?
I don't have any framework language.I want to learn the mvc pattern. I want to know which framework is easy level because I always benefited by stack team.According to me stack team will give easy way for me.
For ex:http://academy.in/services.php I have change to http://www.academy.in/services
You need to create a .htaccess file
A bit like this....
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^/?services/$ services.php [NC,L]
This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 8 years ago.
I am creating a website for my organization that also has some web applications that run server side via CGI using Python. I am wanting a little bit more of a intuitive front end framework. I am also more familiar with Scripting languages then web languages. I have chosen Bootstrap as my frame work for it's user base and bound of documentation.
Now I am understanding the website authentication in regards to PHP and mySQL. But I have a question that I have not been able to find the answer to.
How do I set up website authentication via PHP and mySQL but not have every page end in .php?
For example is it possible to somehow insert this CheckLogin code at the beginning of but inside the html code of a webpage that is .html instead of .php:
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
The CheckLogin code:
function CheckLogin()
{
session_start();
$sessionvar = $this->GetLoginSessionVar();
if(empty($_SESSION[$sessionvar]))
{
return false;
}
return true;
I am referencing this tutorial:http://www.html-form-guide.com/php-form/php-login-form.html
Update: I am using Apache.
I am looking for some guidance besides just the name of the technology, something with a example or tutorial as again I am very new to webside programming and I would benefit most from some real world type of examples.
EDIT: I am also looking for alternatives that would be best suited for CGI type of websites. Anything in the realm of Apache and website authentication based around integration with CGI.
This question was marked as duplicate but I disagree as it has more caveats do to the CGI nature instead of how to simply insert PHP into HTML. The most common solution found when researching "Web site Authentication" is PHP. Hence why asking how to make a CGI website turn by turning PHP pages into HTML pages for the sake of simplicity of using CGI. If there are easier ways to do this by all means please share.
There's a few alternatives. One is to have Apache parse .html files as .php files by adding this to your .htaccess:
AddType application/x-httpd-php .html
Then you could simply add your script to the top of your .html files and it would work. However a cleaner solution would be to use mod_rewrite. For example, you could route www.example.com/page to www.example.com/page.php with
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ $1.php
Or you could have one file as the router:
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ index.php?page=$1
This version would pass whatever comes after www.example.com/ as a $_GET['page'] variable to your index.php file.
Note: I didn't test these, but they should work.
Here's a pretty nice tutorial for some more info: http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
as Halcyon wrote, it is not possible to combine your Authentification with .php and .html, because in .html you can't extract the session vars.
But as he wrote, you can use mod_rewrite to display the page not as ".php" but as ".html".
Regards
Mike
This question already has answers here:
How can I remove file extension from a website address?
(11 answers)
Closed 9 years ago.
I have a build a few sites for my website. They all end with .php.
The problem is if you want to view the page you have to type in website.com/page.php instead of website.com/page. How do I make this happen for all of my main pages?
Is there a quick way of doing this, or do you have to set up a forwarding for all of the /pages to the /page.php?
In most cases this is achieved using MVC framework and Routing. It works in a way that you don't access single .php file for single web page you show to user. Every request goes through one file and you have a router where you define your routes and then define what action controller would that route invoke, and from there you choose what view file will you show to the user. Its hard to explain in few sentances. Anyway using MVC you get nice URL-s like www.example.com/controller/action/param
Now if you just want to remove .php extension from your files you can put this in your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But other then hiding .php, it wont do any good.
The best thing you can do is to read about MVC frameworks, Routing and Front Controller pattern, and take it from there, its not all about nice URL-s, there's much more to gain! And if you just want to hide .php extension then use above code.
Hope this helps!
You save your first file as index.php (index.php is the default page) and include or redirect all the other files internally. So there would be no reason to type a file name.
You can also use apache on .htaccess to rewrite your files, but you have to be careful with this.
Is there a way to hide the fact that I'm using PHP from my users? I wanted to do this for two reasons:
1) So the links in the address bar look cleaner (like here on stackoverflow)
2) To prevent potential hackers of knowing immediately what to look for
Is point 2 even realistic or will a hacker know what I'm using anyway? I'm using nginx with php5-fpm.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/
In addition to the mod_rewrite changes, also set expose_php to false: http://www.php.net/manual/en/ini.core.php#ini.expose-php
The above (or perhaps below) answers give info on the technical side, let me answer the moral side:
Don't do it. Point 2 is completely invalid, if someone wants to do harm, this won't stop it. Proper security checks however will. Point 1 is meagerly valid, no one types links anymore these days.
The best way to keep your PHP hidden from public access is to structure your folders accordingly. Best practice is to keep your library and application files at least one level up from the public folder, like:
/application
// application files
/library
// library and vendor files
/public (aka public_html, htdocs etc)
index.php
.htaccess
/css
/images
/js
Use htaccess and mod_rewrite to route requests to the index.php file, which will then dispatch the request to the application.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
This way, you only have a single php file publicly accessible, which merely includes other files not available via any url
1) So the links in the address bar look cleaner (like here on stackoverflow)
mmm. OK
2) To prevent potential hackers of knowing immediately what to look for
Security by obscurity. Trust me, that's not going to slow them down much.
A very valid reason for doing this, however, is so that your website is not tie to a particular development language.
I see several people have already mentioned mod_rewrite. It's one solution - but it's a very complex tool to master. Also, be very careful about embedding CGI variables in the path of the URL - you can quickly break stuff.
A simple solution would be to implement every entry-point php script (i.e. anything with is not an include/require file) as 'index.php' and reference it by it's directory.
Alternatively pick your own file extension and replace the references to .php in the config with your extension.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Remove .php extension with PHP
What's the best way to get rid of .php suffix in url strings so they look pretty?
Thank you in advance;-)
Use apache mod_rewrite (rewriting rules)
http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
Make sure your apache installation has mod_rewrite enabled (will be in httpd.conf, or one of the files linked there, mods-enabled or such) and look into how routing works in cakePHP.
Couple of tips - the rewrite rules are found in the .htaccess files (make sure you don't have a unicode BOM if the server gives a 500 error) and if you do find you need those $_GET paramters, [qsappend] on your rewrite rule should pass them along. If you still get 500s the compilation errors on regexes can be found in apache's error log, invaluable for debugging.
Might be easier to do a simple project with mod_rewrite first, to learn how it works, as the combination of rewrite and routing in cake can get pretty complex pretty fast.
Options +MultiViews
in the Apache configuration.
Here is a gentle introduction into mod_rewrite.
The best way to do so (at least for me) is:
Use just one file to receive all request. In most of the cases it will be the index.php file.
Then, use mod_rewrite rules like this:
:
RewriteEngine On
RewriteBase /the_base_dir_of_your_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /the_base_dir_of_your_app/index.php [L]
Then, you can analize the URL using functions like basename($_SERVER['REQUEST_URI']); in order to decide what to do.
Use mod_rewrite - or start using ASP.NET MVC 2 :)
If you use a framework, like CakePHP (or any other) it will do it for you. For free. Right now.
.htaccess:
Permalinks
RewriteEngine on
Remove www
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
Links
RewriteRule ^faq$ /faq.php [L]
RewriteRule ^donations$ /donations.php [L]
RewriteRule ^contact$ /contact.php [L]
so they look pretty?
Beauty is in the eye of the beholder. It depends when you consider 'pretty'. A lot also depends on how much you want to get away from the conventions that make a working system possible and the constraints in terms of reconfiguring your site.
While others have mentioned using mod_rewrite, or URL parsing or other such approaches I'm not a fan of these - in addition to being very specific to the type of webserver the code is running on they also break the simple 1:1 mapping beween paths in URIs and paths on the webserver's filesystem.
You could just substitute '.php' with an extension of your choice...but that hardly meets my interpretation of 'pretty'.
The approach I take is to have every script (or at least every script with is intended to be entry point to generaeing a web page) is named as index.php and exists in its own uniquely named directory. The main reason for doing this is nothing to do with making the URL look nice but rather to make the codebase more manageable - I also have strict standards about the naming and placement of include files.
HTH
C.