How to make .php extension not appear on website? [duplicate] - php

This question already has an answer here:
Pretty URLs in PHP frameworks
(1 answer)
Closed 8 years ago.
I am using PHP for my website, and .php is appearing in my URLs. How can I remove this?

There are lots of ways you could achieve this, but they depend largely on your choice of web server.
If, for instance, you were using Apache HTTPD you could use:
MultiViews or
AddHandler (e.g. inside Files or FilesMatch) or
Alias or
mod_rewrite
… other options are probably available, but those are the ones that spring to mind.

Create a file called .htaccess ( the dot in .htaccess is not an accident ) and enter the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
Then save the file and place it a the root directory of your website.

Related

URL structure /page.php vs. /page in PHP [duplicate]

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.

Rewrite PHP all GET parameters .htaccess [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod_rewrite, php and the .htaccess file
Would like this to happen in the .htaccess file.
to rewrite
index.php?pagetype=name
to
/pagetype/name/
do this for all pagetypes so
?user=name
?post=number
?life=sucks ... etc
but would like to exclude
/css/
/images/
/js/
/php/
/template/
so the variable links still work.
I am guessing this should work but I want it in it's most simplest form so I don't have to change the .htaccess file everytime I add a pagetype.
I did a search for this but didn't find anything helpful.
The site will only have one physically accessed file: index.php then the rest are processed via $_GET commands
Just for the example you specified, the simplest approach would be:
RewriteCond %{REQUEST_URI} !^/(css|images|js|php|template)/
RewriteRule ^(\w+)/(\w+)/?$ index.php?$1=$2 [QSA,L]
(The RewriteCond could be written as negative lookeahead in the Rule instead of course.)
See also the mod-rewrite tag wiki for further howtos.

Clean URL using PHP or Apache redirect [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect *.php to clean URL
I am sure this has been asked here before, but for some reason whatever I am trying doesn't seem to work.
What I have is:
http://example.com/share/edit.php?id=59
What I want is:
http://example.com/share/59
My .htaccess is:
RewriteEngine on
RewriteRule ^share/([0-9]+)$ share/edit.php?id=$1
Is there something I am doing wrong? The .htaccess file is in example.com/share directory.
If the .htaccess is in the share directory then you don't need to specify the URL path. Try something like this:
RewriteEngine On
RewriteRule ^(.*)$ edit.php?id=$1 [QSA,L]
It also goes without saying that should you be using apache you will need mod_rewrite enabled.
Try to put your .htaccess to example.com directory.
Try giving it this way:
RewriteEngine On
RewriteRule ^share/([^/]*)$ /share/edit.php?id=$1 [L]
And your .htaccess file should reside in the root directory, i.e., example.com's directory,. where the share folder is there.
The '.htaccess' file is in 'example.com/share' directory.
Move it to the upper level directory (public_html or www).

Clean URL with php and apache [duplicate]

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.

Technique to create user friendly URLs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to create friendly URL in php?
I have read a lot of content on Google about turning
profile.php?id=24 into something more user friendly.
How would I go about redirecting a link like
www.site.com/profile/username
to
profile.php?id=userid (find from username)
Thanks
This can be achieved with the Apache mod_rewrite RewriteEngine. An example .htaccess:
RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
You can do that with apache rewrite rules.
Apache will internally rewrite a URL like /profile/username to profile.php?username=username.
Example: (place this in a .htaccess in the same directory than profile.php)
RewriteEngine On
RewriteRule ^/profile/(.*)$ profile.php?username=$1
If you profile.php script doesn't accept a username parameter, you could also include the user id in the user friendly url:
RewriteEngine On
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2
This will rewrite urls like /profile/username-id to profile.php?id=id
See apahe mod_rewrite documentation.
This is done by creating a RewriteRule in a .htaccess file, of by defining rules in httpd.conf.
This works on Apache. I'm, not sure how to do this on other servers.
When your users sign up, use a PHP or CGI script to make a file called "Username.txt" and store it in a folder called Profiles (as you have it). And inside the text file, make it generate the number (counting up or hashed?) Or use a rewrite service in Google or Apache.

Categories