user2897690
user2897690

Reputation:

hide file extension in url by htaccess

I want to hide my file extensions in browser to be displayed, like http://www.example.com/dir/abc.php should be displayed as http://www.example.com/dir/abc only.

i wrote following rewriteCond and rewrite rule on .htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

but this code doeesn't seems to helping me.

EDIT: this is complete code of my htaccess file

Options -Indexes
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1.*$ [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(js|css|jpg|png)$ - [F]
RewriteEngine on
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]

RewriteEngine On



# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

Upvotes: 1

Views: 5126

Answers (5)

user4400167
user4400167

Reputation:

The simplest and easiest way:

RewriteEngine On

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

Upvotes: 0

php
php

Reputation: 4415

easy method is:

you create your  link like

'<a href="home">Home</a>' instead of '<a href="home.php">Home</a>'
Then your .htaccess should be like this:

RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteCond ^home home.php [L]

Upvotes: 0

CristosLC
CristosLC

Reputation: 422

I'm not an Apache expert by any stretch, but I'm curious why no one is recommending the negotiation module (mod_negotiation). Are there best-practice reasons to avoid using

I heard about it in passing at http://www.webhostingtalk.com/showthread.php?t=317269. After enabling mod_negotiation in my global configuration:

LoadModule negotiation_module /usr/lib/apache2/modules/mod_negotiation.so

I just had to enable it in my virtualhost's Options. Here's the whole config block:

DocumentRoot /var/www/site_directory/public_html
ServerName your.domain.here.com
<Directory /var/www/site_directory/public_html>
allow from all
Options +Indexes Multiviews
</Directory>

Upvotes: 0

anubhava
anubhava

Reputation: 785128

COMPLETE .htaccess:

Options +FollowSymLinks -MultiViews

ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php

RewriteEngine on 

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

# block direct hot linking
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteRule \.(js|css|jpg|png)$ - [F]

RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]

Upvotes: 0

Nitin Karale
Nitin Karale

Reputation: 797

 RewriteEngine On

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

Upvotes: 0

Related Questions