doujian1954 2013-03-04 13:07
浏览 68
已采纳

.htaccess重定向除css和javascript之外的所有扩展名

I have a .htaccess file that redirect any extension to a non-extension url and displays the requestedfilename + .php. It works great with the (.*) part of the conditions.

When I type domain.com/file.html or domain.com/file.xml it displays the file.php and the url looks like domain.com/file.

I just would like to know how to exculde extensions like .js and .css from the expression. I don't want to redirect them to any other php file.

I tried things like: (.*!.(js|css)) instead of (.*) but I can't find a working solution...

The current code is this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#
# to display the name.php file for any requested extension (.*)
#
RewriteRule ^(.*)\.(.*) $1\.php

#
# to hide all type of extensions (.*) of urls
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.(.*)\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]

#
# no extension url to php
#
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

</IfModule>
  • 写回答

2条回答 默认 最新

  • duanmei4149 2013-03-04 13:28
    关注

    If your version of Apache supports it, you may be able to use "negative lookahead" and write the first RewriteRule like this:

    RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
    

    The [^.]part makes sure the (.*)\. matches everything until the last ., "positioning" the negative lookahead at the right spot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用