Jump to content

How To Remove .php - Why So Many Options?


justlukeyou

Recommended Posts

Hi,

 

I am trying to remove the .php from the end of pages.

 

The problem I have tried around ten different options and none of them work, but how come there are so many different options?

 

http://stackoverflow.com/questions/9821222/remove-php-extension-explicitly-written-for-friendly-url

http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/

 

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^([^\.]+)$ $1.php [NC,L]

Link to comment
Share on other sites

Are you sure you save it as .htaccess?

 

If you're working on localhost, make sure your apache is rewritable

I have similar problem before

 

http://phpcollection.com/mod-rewrite-windows-wamp-xampp/ - windows

http://blog.valugi.ro/2008/05/29/htaccess_internal_server_error_apache2_ubuntu/ - ubuntu

 

Or if you want an easy solution, try putting each pages into folder and rename the files to index.php

For example the about.php page will be about/index.php, so whenever you link to about page it will load the page without the "index.php"

Link to comment
Share on other sites

Thanks,

 

This is what Im currently using. How can I get the apache version?

 

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/$ $1.html

RewriteCond %{REQUEST_FILENAME}.py -f
RewriteRule ^(.*)/$ $1.py
Link to comment
Share on other sites

You can use the code below which will give you URLs like this: ht tp://www.domain.com/somepage

RewriteEngine On
RewriteBase /
RewriteRule ^index$ index.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^search$ search.php [QSA,L]
RewriteRule ^privacy-policy$ privacy.php [QSA,L]
RewriteRule ^terms-of-service$ terms.php [QSA,L]

Link to comment
Share on other sites

@justlukeyou, you can apply multiple rules to the same document, but....it's not allowed to redirect the request to the same page!

 

Solution:

 

Aplly fake urls and redirect them to a different (real) directory on the same project.

Link to comment
Share on other sites

You can use the code below which will give you URLs like this: ht tp://www.domain.com/somepage

RewriteEngine On
RewriteBase /
RewriteRule ^index1$ index1.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^search$ search.php [QSA,L]
RewriteRule ^privacy-policy$ privacy.php [QSA,L]
RewriteRule ^terms-of-service$ terms.php [QSA,L]

 

Hi,

 

I tried this but it doesn't work.

 

Jazzman1,

 

Lets say I have a link like www.website.com/index1.php I am very confused by what you by "Aplly fake urls and redirect them to a different (real) directory on the same project."

 

I thought their would be a standard way to do this?

Link to comment
Share on other sites

Alright then, let's doing the test.

 

Assuming the we have the next structure:

/ # web root directory

  /project # the folder of your project

  # in that exampe let's say you have two .php files and one .htaccess

    /project/test.php  #contains <?php echo "project testing file" ?> 

    /project/debug.php #contains <?php echo "project debugging file" ?> 
 
   /project/.htaccess

The content of the .htaccess file should be: 

RewriteEngine on

RewriteCond %{REQUEST_URI}  ^/project/([a-z]+)\.php$

RewriteRule ^(.*)$ /project/%1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.*)$ /project/$1.php [L,QSA]

Now if someone try to achieve for instance /project/test.php you will see that the URL is going to change from /project/test.php to /project/test, but also you will get a message:
 

 

The page isn't redirecting properly

 

b/s you're trying to redirect the same request to the same page.

But now let's make another test.

Delete only the php files inside the project folder and create a new sub-directory for this test we will named it - /project/web

Inside this new web directory create two files test.php and debug.php.

 

And change the rule of .htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI}  ^/project/([a-z]+)\.php$

RewriteRule ^(.*)$ /project/%1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.*)$ /project/web/$1.php [L,QSA]

Now that files are going to be reached as URL's - /project/debug.php or /project/debug even they don't exist inside /project folder.

 

If some smart user try to achieve for instance /project/web/debug.php, you could create a new .htaccess file in this folder with a new rule of a forbidden flag (forbidden|F ) telling Apache not to provide a page in response to this request.

Edited by jazzman1
Link to comment
Share on other sites

Hi,

 

I have a director called "siteinfo" so I tried the following but it had no affect.

 

Can I just check two things:

 

1. Does the file need to save in a certain format. I am just using Notepad and saving it in a test file

 

2. Is the file called "htaccess" or ".htaccess"

 

I have tried both and tried to save the file in different formats but still no impact.

 

    RewriteEngine on
     
    RewriteCond %{REQUEST_URI} ^/siteinfo/([a-z]+)\.php$
     
    RewriteRule ^(.*)$ /siteinfo/%1 [R=301,L]
     
    RewriteCond %{SCRIPT_FILENAME} !-f
     
    RewriteRule ^(.*)$ /siteinfo/$1.php [L,QSA]
Link to comment
Share on other sites

Check Apache conf whether a mod_rewrite module is installed and enabled too.

 

May I ask you something? Is there some reasonably reason to do this?

 

I just provide you en axample, but not sure how mutch effectively an flexible is that if you have a complex web structure.

Edited by jazzman1
Link to comment
Share on other sites

<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
</IfModule>

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

This doesnt work?

 

For me, using XAMPP, this enables:

 

localhost/index

localhost/user

 

Where index.php and user.php are files in my folder.

Edited by DaveyK
Link to comment
Share on other sites

@DaveyK, there is no problem to use urls like /localhost/index poiting to /localhost/index.php.

 

He wants, when the browser send a reques for instance /localhost/index.php the index.php not to be served from Apache. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.