.htaccess隐藏.php文件扩展名,显示/(斜杠)代替

时间:2014-02-27 19:50:35

标签: javascript php .htaccess

我想通过.htaccesss将 filename.php 更改为 filename /

所以,http://labs.ksj-oepites-zomergem.be/info.php#/deleiding 应成为http://labs.ksj-oepites-zomergem.be/info/#/deleiding

我得到了什么:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([-a-zA-Z0-9]+)\/$ $1.php [L]

此代码会将 filename.php 更改为 filename 而不是 filename /

在我的.htaccess中使用此代码,我的网站将无法正常工作。 当我在javascript中使用ajax调用php文件时......我得到了一个'意外的EOF'

有什么建议吗?!

1 个答案:

答案 0 :(得分:0)

重复: hide .php extension - htaccess

答案是:     RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

这是一个副本: hide .php extension - htaccess

有哪些答案:     RewriteRule ^(。*)$ /$1.php [L]

当隐藏PHP时,我倾向于使用后者,添加规则使.asp,.html,.cfm和.jsp也重写为.php,因此在服务器上戳戳不应该揭示技术。也就是说,除非我被客户要求,否则我不倾向于隐藏它,或者我想创建更短/更漂亮的URL。