Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-25-2011, 05:45 PM   PM User | #1
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Hide file extension .htaccess

I've been trying to hide my .php file extension using .htaccess, but I can't seems to get it working. I want to be able to visit my pages without typing the .php part and without .php showing in the url. Like:

http://www.example.com/events.php

vs

http://www.example.com/events

I've been trying the following, but I can't get it to work:

Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It always says page not found.
sankou is offline   Reply With Quote
Old 10-25-2011, 06:09 PM   PM User | #2
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
any ideas why this won't work for me?
sankou is offline   Reply With Quote
Old 10-25-2011, 09:40 PM   PM User | #3
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
I've tried a bunch of different solutions, but I can't make it work. Any help would be great.
sankou is offline   Reply With Quote
Old 10-25-2011, 10:20 PM   PM User | #4
Microsuck
Regular Coder

 
Microsuck's Avatar
 
Join Date: Oct 2011
Location: 127.0.0.1
Posts: 123
Thanks: 44
Thanked 5 Times in 5 Posts
Microsuck is an unknown quantity at this point
Try this.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [QSA]
Microsuck is offline   Reply With Quote
Old 10-25-2011, 10:28 PM   PM User | #5
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Quote:
Originally Posted by Microsuck View Post
Try this.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [QSA]
Thanks for the reply. This does remove the extension, however it doesn't go to the right page. If I type http://www.mysite.com/about it shows that as the url but goes to the index page instead of the about page.
sankou is offline   Reply With Quote
Old 10-25-2011, 10:33 PM   PM User | #6
Microsuck
Regular Coder

 
Microsuck's Avatar
 
Join Date: Oct 2011
Location: 127.0.0.1
Posts: 123
Thanks: 44
Thanked 5 Times in 5 Posts
Microsuck is an unknown quantity at this point
Quote:
Originally Posted by sankou View Post
Thanks for the reply. This does remove the extension, however it doesn't go to the right page. If I type http://www.mysite.com/about it shows that as the url but goes to the index page instead of the about page.
My apologies!

I *think* this will work.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /*.php?$1 [QSA]
If that doesn't work, try this:

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ *.php?$1 [QSA]
I am just learning how to play with htaccess, so sorry if none of this works.
Microsuck is offline   Reply With Quote
Old 10-25-2011, 10:40 PM   PM User | #7
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Quote:
Originally Posted by Microsuck View Post
My apologies!

I *think* this will work.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /*.php?$1 [QSA]
If that doesn't work, try this:

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ *.php?$1 [QSA]
I am just learning how to play with htaccess, so sorry if none of this works.
I tried them, I get a 500 internal service error with both.
sankou is offline   Reply With Quote
Old 10-25-2011, 10:42 PM   PM User | #8
Microsuck
Regular Coder

 
Microsuck's Avatar
 
Join Date: Oct 2011
Location: 127.0.0.1
Posts: 123
Thanks: 44
Thanked 5 Times in 5 Posts
Microsuck is an unknown quantity at this point
Quote:
Originally Posted by sankou View Post
I tried them, I get a 500 internal service error with both.
Give this a try

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ .php?$1 [QSA]
Microsuck is offline   Reply With Quote
Old 10-25-2011, 10:46 PM   PM User | #9
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Quote:
Originally Posted by Microsuck View Post
Give this a try

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ .php?$1 [QSA]
I'm still getting the 500 internal service error
sankou is offline   Reply With Quote
Old 10-25-2011, 10:50 PM   PM User | #10
Microsuck
Regular Coder

 
Microsuck's Avatar
 
Join Date: Oct 2011
Location: 127.0.0.1
Posts: 123
Thanks: 44
Thanked 5 Times in 5 Posts
Microsuck is an unknown quantity at this point
I'm just striking out.

A few more tries before I give up.

Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Microsuck is offline   Reply With Quote
Old 10-25-2011, 10:52 PM   PM User | #11
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
I think I got it working. I switched the last line to

Code:
RewriteRule ^(.*)$ /$1.php
From what I understand, it takes whats after the / and stores it in $1. So if I were to navigate to mysite.com/about the last line forwards it to mysite.com/$1.php. The $1 in this case is about. Is this right?
sankou is offline   Reply With Quote
Old 10-25-2011, 10:54 PM   PM User | #12
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Quote:
Originally Posted by Microsuck View Post
I'm just striking out.

A few more tries before I give up.

Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Yes, I saw this after my last post. Mine just had a slash before $1. Thanks so much for your help!
sankou is offline   Reply With Quote
Old 10-25-2011, 10:56 PM   PM User | #13
Microsuck
Regular Coder

 
Microsuck's Avatar
 
Join Date: Oct 2011
Location: 127.0.0.1
Posts: 123
Thanks: 44
Thanked 5 Times in 5 Posts
Microsuck is an unknown quantity at this point
Quote:
Originally Posted by sankou View Post
Yes, I saw this after my last post. I think this would work as well. Mine just had a slash before $1. Thanks so much for your help!
No problem! Sorry it took so long!
Microsuck is offline   Reply With Quote
Users who have thanked Microsuck for this post:
sankou (10-25-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:58 PM.


Advertisement
Log in to turn off these ads.