Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache - 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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...