有没有办法使用mod_rewrite从我的网站上删除.html和.php代码?
我想知道是否有一种方法,使用mod_rewrite,从我的网站上删除.html和.php代码 我的大多数页面都是用HTML制作的,但是联系人页面的扩展名是.php。使用.htaccess中的代码有没有办法使用mod_rewrite从我的网站上删除.html和.php代码?,php,html,mod-rewrite,Php,Html,Mod Rewrite,我想知道是否有一种方法,使用mod_rewrite,从我的网站上删除.html和.php代码 我的大多数页面都是用HTML制作的,但是联系人页面的扩展名是.php。使用.htaccess中的代码 Options +FollowSymLinks Options +Indexes RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.html [NC,L] 显然,它会处理其中的.html部分。有没
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
显然,它会处理其中的.html部分。有没有改变这一点,包括我的一个php页面以及
或者,如果没有其他方法,是否有一种方法可以分别为每个页面编写mod_重写代码,以摆脱它们的扩展
先谢谢你 您可以将重写规则加倍,并检查每个可能的扩展名(如果存在具有该名称的文件,则规则将生效)
$
regex的'zero width'与当前路径的末尾匹配,因此只需添加扩展名
最省力的解决方案就是使用:
Options +MultiViews
(实际上启用了更多的服务器智能,但带来了轻微的性能成本。)您可以将重写规则加倍,并检查每个可能的扩展名(如果存在具有该名称的文件,则规则将生效)
$
regex的'zero width'与当前路径的末尾匹配,因此只需添加扩展名
最省力的解决方案就是使用:
Options +MultiViews
(实际上可以实现更多的服务器智能,但性能成本很低。)如果您只有一个静态站点,那么您可能可以使用此站点:
RewriteRule ^(my-contact-page-name)$ $1.php [L,NC]
RewriteRule ^([^\.]+)$ $1.html [L,NC]
只需确保在没有
.php
:)的情况下输入我的联系人页面名
)如果您只是一个静态站点,那么您可能可以使用此站点:
RewriteRule ^(my-contact-page-name)$ $1.php [L,NC]
RewriteRule ^([^\.]+)$ $1.html [L,NC]
只需确保在没有
.php
:)的情况下输入我的联系人页面名
)以下是一个完全不同的答案:
# Stop if static files:
RewriteRule (.*)(\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico))$ - [NC,QSA,L]
# if it ends by htm or Php, 404:
RewriteRule (.*)(php|htm|html)$ - [L,R=404]
# if, when adding ".htm" it's a file...
RewriteCond %{SCRIPT_FILENAME}.htm -f
# Rewrite it (and end processing):
RewriteRule (.*) $1.htm - [L,R=404]
# Same principles with "html" then php:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule (.*) $1.html - [L,R=404]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule (.*) $1.php - [L,R=404]
# everything else = 404:
RewriteRule . - [L,R=404]
两个提示:
请尝试使用
RewriteLog
指令:它可以帮助您跟踪此类问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
我最喜欢的检查regexp的工具: (别忘了选择ereg(POSIX)而不是preg(PCRE)!)
我可以请您在问题中添加重写日志吗?这里有一个完全不同的答案:
# Stop if static files:
RewriteRule (.*)(\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico))$ - [NC,QSA,L]
# if it ends by htm or Php, 404:
RewriteRule (.*)(php|htm|html)$ - [L,R=404]
# if, when adding ".htm" it's a file...
RewriteCond %{SCRIPT_FILENAME}.htm -f
# Rewrite it (and end processing):
RewriteRule (.*) $1.htm - [L,R=404]
# Same principles with "html" then php:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule (.*) $1.html - [L,R=404]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule (.*) $1.php - [L,R=404]
# everything else = 404:
RewriteRule . - [L,R=404]
两个提示:
请尝试使用
RewriteLog
指令:它可以帮助您跟踪此类问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
我最喜欢的检查regexp的工具: (别忘了选择ereg(POSIX)而不是preg(PCRE)!)
我可以请您在问题中添加rewritelog吗?这将删除扩展,但将重定向到一个页面:index.php 您可以在其中检查页面是否存在并包含它
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [NC,L]
这将去掉扩展,但将重定向到一个页面:index.php 您可以在其中检查页面是否存在并包含它
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [NC,L]