我想在我的.htaccess
文件中添加一些内容,以隐藏我的php文件的.php
文件扩展名,因此访问www.example.com/dir/somepage会向他们展示www.example的.com / DIR / somepage.php。
这有什么可行的解决方案吗?我的网站使用HTTPS,如果这很重要的话。
这是我的.htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
ErrorDocument 404 /error/404.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
答案 0 :(得分:38)
在DOCUMENT_ROOT:
下的.htaccess中使用此代码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]
答案 1 :(得分:4)
删除php扩展程序
您可以使用/.htaccess中的代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [NC,L]
使用上面的代码,您将能够将/file.php作为/ file
进行访问删除html扩展程序
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [NC,L]
注意:'rewriteEngine on'指令应该在每个htaccess的顶部使用一次,所以如果要组合这两个规则,只需从第二个规则中删除该行。您还可以删除您选择的任何其他扩展,只需在代码上替换.php即可。
快乐htaccessing!
答案 2 :(得分:2)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # if the requested URL is not a file that exists
RewriteCond %{REQUEST_FILENAME} !-d # and it isn't a directory that exists either
RewriteCond %{REQUEST_FILENAME}\.php -f # but when you put ".php" on the end it is a file that exists
RewriteRule ^(.+)$ $1\.php [QSA] # then serve that file (maintaining the query string)
</IfModule>
奇怪的是,这个代码在Windows中的XAMPP中尝试时给出了HTTP 500错误。删除注释修复它。因此,移动评论以使它们按照自己的方式排列,而不是与说明相同。
答案 3 :(得分:0)
试试这个:
# 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]Try:
参考: