删除扩展名

时间:2016-09-20 18:33:20

标签: regex apache .htaccess mod-rewrite

行。所以我现在正在建立一个网站来显示图像。 (有点像Imgur我猜),正常的URL是/ n?= image,现在我有一个重写来改变它/ image,这很好。

我还有一个隐私政策,ToS和联系页面,我想从(.php)删除扩展名。 “隐私策略”页面工作正常,因为该文件位于名为“策略”的目录中。所以它删除.php扩展没有麻烦。

当我去/联系时,它重定向它认为它是一个图像。是否可以删除.php扩展名,以及同时重定向图像?我在这里搜索过但找不到任何与此问题有关的内容。

# Snapr Rewrites
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)$ index.php?n=$1
RewriteRule ^([a-zA-Z0-9]+).png$ index.php?n=$1
RewriteRule ^([a-zA-Z0-9]+)$ ^([a-zA-Z0-9]+).png$

# Remove .PHP from URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

编辑:

基本上。我想要:

http://example.com/?n=image变成http://example.com/image

http://example.com/contact.php变成http://example.com/contact

1 个答案:

答案 0 :(得分:1)

在为请求添加.php之前,您应检查是否存在.php文件,更重要的是在其他图像规则之前保留.php添加规则:

RewriteEngine On
RewriteBase /

# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# handle images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)(?:\.png)?$ index.php?n=$1 [L,QSA]
相关问题